diff --git a/Common/PdfScribeSharedAssemblyInfo.cs b/Common/PdfScribeSharedAssemblyInfo.cs index df9d40c..ab34627 100644 --- a/Common/PdfScribeSharedAssemblyInfo.cs +++ b/Common/PdfScribeSharedAssemblyInfo.cs @@ -5,7 +5,7 @@ // associated with an assembly. [assembly: AssemblyCompany("Black Telescope Workshop")] [assembly: AssemblyProduct("PDF Scribe")] -[assembly: AssemblyCopyright("Copyright © S T Chan 2013-2018")] +[assembly: AssemblyCopyright("Copyright © S T Chan 2013-2019")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -22,4 +22,4 @@ // Build Number // Revision // -[assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.7.*")] diff --git a/PdfScribe/App.config b/PdfScribe/App.config index 7b2e10d..742d0a4 100644 --- a/PdfScribe/App.config +++ b/PdfScribe/App.config @@ -1,43 +1,43 @@ - - - - -
- - - - - - - - + + + + +
+ + + + + + + + - + switchName="PdfScribeAll" > + - - - - - - - - - - + traceOutputOptions="DateTime" /> + + + + + + + + + + - - %UserProfile%\PDFSCRIBE.PDF - - - True - - - False - - - - + + %UserProfile%\PDFSCRIBE.PDF + + + False + + + True + + + + diff --git a/PdfScribe/Program.cs b/PdfScribe/Program.cs index c0e7829..1395544 100644 --- a/PdfScribe/Program.cs +++ b/PdfScribe/Program.cs @@ -1,10 +1,7 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Drawing; using System.IO; using System.Runtime.InteropServices; -using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; @@ -16,13 +13,16 @@ namespace PdfScribe #region Message constants - + const string errorDialogCaption = "PDF Scribe"; // Error taskdialog caption text - + const string errorDialogInstructionPDFGeneration = "There was a PDF generation error."; const string errorDialogInstructionCouldNotWrite = "Could not create the output file."; const string errorDialogInstructionUnexpectedError = "There was an internal error. Enable tracing for details."; + const string errorDialogOutputFilenameInvalid = "Output file path is not valid. Check the \"OutputFile\" setting in the config file."; + const string errorDialogOutputFilenameTooLong = "Output file path too long. Check the \"OutputFile\" setting in the config file."; + const string errorDialogOutputFileAccessDenied = "Access denied - check permissions on output folder."; const string errorDialogTextFileInUse = "{0} is being used by another process."; const string errorDialogTextGhostScriptConversion = "Ghostscript error code {0}."; @@ -33,7 +33,7 @@ namespace PdfScribe #region Other constants const string traceSourceName = "PdfScribe"; - const string defaultOutputFilename = "PDFSCRIBE.PDF"; + //const string defaultOutputFilename = "PDFSCRIBE.PDF"; #endregion @@ -76,7 +76,7 @@ namespace PdfScribe { // We couldn't delete, or create a file // because it was in use - logEventSource.TraceEvent(TraceEventType.Error, + logEventSource.TraceEvent(TraceEventType.Error, (int)TraceEventType.Error, errorDialogInstructionCouldNotWrite + Environment.NewLine + @@ -91,8 +91,8 @@ namespace PdfScribe // because it was set to readonly // or couldn't create a file // because of permissions issues - logEventSource.TraceEvent(TraceEventType.Error, - (int)TraceEventType.Error, + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, errorDialogInstructionCouldNotWrite + Environment.NewLine + "Exception message: " + unauthorizedEx.Message); @@ -105,8 +105,8 @@ namespace PdfScribe catch (ExternalException ghostscriptEx) { // Ghostscript error - logEventSource.TraceEvent(TraceEventType.Error, - (int)TraceEventType.Error, + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString()) + Environment.NewLine + "Exception message: " + ghostscriptEx.Message); @@ -121,7 +121,7 @@ namespace PdfScribe { File.Delete(standardInputFilename); } - catch + catch { logEventSource.TraceEvent(TraceEventType.Warning, (int)TraceEventType.Warning, @@ -152,7 +152,7 @@ namespace PdfScribe bool filenameRetrieved = false; switch (Properties.Settings.Default.AskUserForOutputFilename) { - case (true) : + case (true): using (SetOutputFilename dialogOwner = new SetOutputFilename()) { dialogOwner.TopMost = true; @@ -179,8 +179,47 @@ namespace PdfScribe } break; default: - outputFile = GetOutputFilename(); - filenameRetrieved = true; + try + { + outputFile = GetOutputFilename(); + // Test if we can write to the destination + using (FileStream newOutputFile = File.Create(outputFile)) + { } + File.Delete(outputFile); + filenameRetrieved = true; + } + catch (Exception ex) when (ex is ArgumentException || + ex is ArgumentNullException || + ex is NotSupportedException || + ex is DirectoryNotFoundException) + { + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + errorDialogOutputFilenameInvalid + Environment.NewLine + + "Exception message: " + ex.Message); + DisplayErrorMessage(errorDialogCaption, + errorDialogOutputFilenameInvalid); + } + catch (PathTooLongException ex) + { + // filename is greater than 260 characters + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + errorDialogOutputFilenameTooLong + Environment.NewLine + + "Exception message: " + ex.Message); + DisplayErrorMessage(errorDialogCaption, + errorDialogOutputFilenameTooLong); + } + catch (UnauthorizedAccessException ex) + { + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + errorDialogOutputFileAccessDenied + Environment.NewLine + + "Exception message: " + ex.Message); + // Can't write to target dir + DisplayErrorMessage(errorDialogCaption, + errorDialogOutputFileAccessDenied); + } break; } return filenameRetrieved; @@ -189,58 +228,15 @@ namespace PdfScribe private static String GetOutputFilename() { - - String outputFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), defaultOutputFilename); - if (!String.IsNullOrEmpty(Properties.Settings.Default.OutputFile) && - !String.IsNullOrWhiteSpace(Properties.Settings.Default.OutputFile)) - { - if (IsFilePathValid(Properties.Settings.Default.OutputFile)) - { - outputFilename = Properties.Settings.Default.OutputFile; - } - else - { - if (IsFilePathValid(Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile))) - { - outputFilename = Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile); - } - } - } - else - { - logEventSource.TraceEvent(TraceEventType.Warning, - (int)TraceEventType.Warning, - String.Format("Using default output filename {0}", - outputFilename)); - } + String outputFilename = Path.GetFullPath(Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile)); + // Check if there are any % characters - + // even though it's a legal Windows filename character, + // it is a special character to Ghostscript + if (outputFilename.Contains("%")) + throw new ArgumentException("OutputFile setting contains % character."); return outputFilename; } - static bool IsFilePathValid(String filePath) - { - bool pathIsValid = false; - - if (!String.IsNullOrEmpty(filePath) && filePath.Length <= 260) - { - String directoryName = Path.GetDirectoryName(filePath); - String filename = Path.GetFileName(filePath); - - if (Directory.Exists(directoryName)) - { - // Check for invalid filename chars - Regex containsABadCharacter = new Regex("[" - + Regex.Escape(new String(System.IO.Path.GetInvalidPathChars())) + "]"); - pathIsValid = !containsABadCharacter.IsMatch(filename); - } - } - else - { - logEventSource.TraceEvent(TraceEventType.Warning, - (int)TraceEventType.Warning, - "Output filename is longer than 260 characters, or blank."); - } - return pathIsValid; - } /// /// Opens the PDF in the default viewer @@ -273,7 +269,7 @@ namespace PdfScribe MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); - + } } } diff --git a/PdfScribe/Properties/Settings.Designer.cs b/PdfScribe/Properties/Settings.Designer.cs index 77f2ad8..2f98a67 100644 --- a/PdfScribe/Properties/Settings.Designer.cs +++ b/PdfScribe/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace PdfScribe.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.7.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -32,15 +32,6 @@ namespace PdfScribe.Properties { } } - [global::System.Configuration.ApplicationScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("False")] - public bool AskUserForOutputFilename { - get { - return ((bool)(this["AskUserForOutputFilename"])); - } - } - [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("False")] @@ -49,5 +40,14 @@ namespace PdfScribe.Properties { return ((bool)(this["OpenAfterCreating"])); } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool AskUserForOutputFilename { + get { + return ((bool)(this["AskUserForOutputFilename"])); + } + } } } diff --git a/PdfScribe/Properties/Settings.settings b/PdfScribe/Properties/Settings.settings index 26b34dd..ff31d6c 100644 --- a/PdfScribe/Properties/Settings.settings +++ b/PdfScribe/Properties/Settings.settings @@ -5,11 +5,11 @@ %UserProfile%\PDFSCRIBE.PDF - - False - False + + True + \ No newline at end of file diff --git a/PdfScribeInstall/Product.wxs b/PdfScribeInstall/Product.wxs index ece7957..57819d4 100644 --- a/PdfScribeInstall/Product.wxs +++ b/PdfScribeInstall/Product.wxs @@ -1,11 +1,11 @@ - +