From bebd81eefa159e5eb50dd6a109a823aff8f9737d Mon Sep 17 00:00:00 2001 From: S T Chan Date: Thu, 19 Dec 2013 21:40:17 -0500 Subject: [PATCH] More refactoring and tracing added --- PdfScribe/ActivityNotificationPresenter.cs | 8 ++- PdfScribe/ErrorDialogPresenter.cs | 12 ++-- PdfScribe/Program.cs | 49 ++++++++++---- PdfScribeCore/PdfScribeInstaller.cs | 75 +++++++++++++++++----- 4 files changed, 109 insertions(+), 35 deletions(-) diff --git a/PdfScribe/ActivityNotificationPresenter.cs b/PdfScribe/ActivityNotificationPresenter.cs index 56dd505..3cf72f1 100644 --- a/PdfScribe/ActivityNotificationPresenter.cs +++ b/PdfScribe/ActivityNotificationPresenter.cs @@ -19,7 +19,9 @@ namespace PdfScribe private Application activityWindow = null; /// - /// + /// Displays the floating frameless + /// activity notification window on + /// a separate thread /// public void ShowActivityNotificationWindow() { @@ -39,8 +41,8 @@ namespace PdfScribe } /// - /// Shuts down the thread showing - /// the ActivityNotification WPF window + /// Shuts down the WPF Application showing + /// the ActivityNotification window /// public void CloseActivityNotificationWindow() { diff --git a/PdfScribe/ErrorDialogPresenter.cs b/PdfScribe/ErrorDialogPresenter.cs index 308c0b4..5be54dd 100644 --- a/PdfScribe/ErrorDialogPresenter.cs +++ b/PdfScribe/ErrorDialogPresenter.cs @@ -21,9 +21,9 @@ namespace PdfScribe /// Ctor overload that shows the /// task dialog immediately /// - /// - /// - /// + /// Text that goes in the window caption + /// Instructional text (Appears next to the icon) + /// Smaller message detail text at bottom public ErrorDialogPresenter(String captionText, String instructionText, String messageText) @@ -38,9 +38,9 @@ namespace PdfScribe /// with a standard error icon, and /// just a Close button /// - /// - /// - /// + /// Text that goes in the window caption + /// Instructional text (Appears next to the icon) + /// Smaller message detail text at bottom public void ShowSimple(String captionText, String instructionText, String messageText) diff --git a/PdfScribe/Program.cs b/PdfScribe/Program.cs index 072da01..5169566 100644 --- a/PdfScribe/Program.cs +++ b/PdfScribe/Program.cs @@ -14,8 +14,10 @@ namespace PdfScribe { - #region Error messages + #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 unexpected, and unhandled error in PDF Scribe."; @@ -23,6 +25,8 @@ namespace PdfScribe const string errorDialogTextFileInUse = "{0} is being used by another process."; const string errorDialogTextGhostScriptConversion = "Ghostscript error code {0}."; + const string warnFileNotDeleted = "{0} could not be deleted."; + #endregion #region Other constants @@ -69,26 +73,41 @@ namespace PdfScribe { // We couldn't delete, or create a file // because it was in use + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + errorDialogInstructionCouldNotWrite + + Environment.NewLine + + "Exception message: " + ioEx.Message); ErrorDialogPresenter errorDialog = new ErrorDialogPresenter(errorDialogCaption, - errorDialogInstructionCouldNotWrite, - String.Empty); + errorDialogInstructionCouldNotWrite, + String.Empty); } - catch (UnauthorizedAccessException) + catch (UnauthorizedAccessException unauthorizedEx) { // Couldn't delete a file // because it was set to readonly // or couldn't create a file // because of permissions issues + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + errorDialogInstructionCouldNotWrite + + Environment.NewLine + + "Exception message: " + unauthorizedEx.Message); ErrorDialogPresenter errorDialog = new ErrorDialogPresenter(errorDialogCaption, - errorDialogInstructionCouldNotWrite, - String.Empty); + errorDialogInstructionCouldNotWrite, + String.Empty); } catch (ExternalException ghostscriptEx) { // Ghostscript error + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString()) + + Environment.NewLine + + "Exception message: " + ghostscriptEx.Message); ErrorDialogPresenter errorDialog = new ErrorDialogPresenter(errorDialogCaption, - errorDialogInstructionPDFGeneration, - String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString())); + errorDialogInstructionPDFGeneration, + String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString())); } finally @@ -97,7 +116,12 @@ namespace PdfScribe { File.Delete(standardInputFilename); } - catch {} + catch + { + logEventSource.TraceEvent(TraceEventType.Warning, + (int)TraceEventType.Warning, + String.Format(warnFileNotDeleted, standardInputFilename)); + } userDisplay.CloseActivityNotificationWindow(); } } @@ -109,9 +133,12 @@ namespace PdfScribe /// static void Application_UnhandledException(object sender, UnhandledExceptionEventArgs e) { + logEventSource.TraceEvent(TraceEventType.Critical, + (int)TraceEventType.Critical, + ((Exception)e.ExceptionObject).Message); ErrorDialogPresenter errorDialog = new ErrorDialogPresenter(errorDialogCaption, - errorDialogInstructionUnexpectedError, - String.Empty); + errorDialogInstructionUnexpectedError, + String.Empty); } diff --git a/PdfScribeCore/PdfScribeInstaller.cs b/PdfScribeCore/PdfScribeInstaller.cs index 65155b1..5e04a4b 100644 --- a/PdfScribeCore/PdfScribeInstaller.cs +++ b/PdfScribeCore/PdfScribeInstaller.cs @@ -70,6 +70,8 @@ namespace PdfScribeCore const string FILENOTDELETED_INUSE = "{0} is being used by another process. File was not deleted."; const string FILENOTDELETED_UNAUTHORIZED = "{0} is read-only, or its file permissions do not allow for deletion."; + const string FILENOTCOPIED_PRINTERDRIVER = "Printer driver file was not copied. Exception message: {0}"; + const string WIN32ERROR = "Win32 error code {0}."; const string NATIVE_COULDNOTENABLE64REDIRECTION = "Could not enable 64-bit file system redirection."; @@ -84,7 +86,11 @@ namespace PdfScribeCore { this.logEventSource = new TraceSource(logEventSourceNameDefault); } - + /// + /// This override sets the + /// trace source to a specific name + /// + /// Trace source name public PdfScribeInstaller(String eventSourceName) { if (!String.IsNullOrEmpty(eventSourceName)) @@ -124,7 +130,8 @@ namespace PdfScribeCore /// /// /// - /// I can't remember the name of the coder who wrote this code originally + /// I can't remember the name of the developer who wrote this code originally, + /// so I can't provide a link or credit. private int DoXcvDataPortOperation(string portName, string portMonitor, string xcvDataOperation) { @@ -205,10 +212,22 @@ namespace PdfScribeCore newMonitor.pEnvironment = ENVIRONMENT_64; newMonitor.pDLLName = MONITORDLL; if (!AddPortMonitor(newMonitor)) - throw new Win32Exception(Marshal.GetLastWin32Error(), String.Format("Could not add port monitor {0}", PORTMONITOR)); + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + String.Format("Could not add port monitor {0}", PORTMONITOR) + Environment.NewLine + + String.Format(WIN32ERROR, Marshal.GetLastWin32Error().ToString())); else monitorAdded = true; } + else + { + // Monitor already installed - + // log it, and keep going + logEventSource.TraceEvent(TraceEventType.Warning, + (int)TraceEventType.Warning, + String.Format("Port monitor {0} already installed.", PORTMONITOR)); + monitorAdded = true; + } } finally @@ -519,25 +538,51 @@ namespace PdfScribeCore { File.Copy(fileSourcePath, fileDestinationPath); } + catch (PathTooLongException) + { + // Will be caught by outer + // IOException catch block + throw; + } + catch (DirectoryNotFoundException) + { + // Will be caught by outer + // IOException catch block + throw; + } + catch (FileNotFoundException) + { + // Will be caught by outer + // IOException catch block + throw; + } catch (IOException) { - // Just keep going - file was already - // there, but we didn't overwrite + // Just keep going - file was already there + // Not really a problem continue; } } filesCopied = true; } - catch (UnauthorizedAccessException) - { } - catch (PathTooLongException) - { } - catch (DirectoryNotFoundException) - { } - catch (FileNotFoundException) - { } - catch (NotSupportedException) - { } + catch (IOException ioEx) + { + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + String.Format(FILENOTCOPIED_PRINTERDRIVER, ioEx.Message)); + } + catch (UnauthorizedAccessException unauthorizedEx) + { + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + String.Format(FILENOTCOPIED_PRINTERDRIVER, unauthorizedEx.Message)); + } + catch (NotSupportedException notSupportedEx) + { + logEventSource.TraceEvent(TraceEventType.Error, + (int)TraceEventType.Error, + String.Format(FILENOTCOPIED_PRINTERDRIVER, notSupportedEx.Message)); + } return filesCopied;