Skip to content

Commit

Permalink
Merge pull request #395 from Tichau/integration
Browse files Browse the repository at this point in the history
File Converter release v2.0.1
  • Loading branch information
Tichau authored Mar 10, 2024
2 parents 044ea6d + 2088fa2 commit 58228da
Show file tree
Hide file tree
Showing 28 changed files with 1,460 additions and 353 deletions.
74 changes: 39 additions & 35 deletions Application/FileConverter/Application.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace FileConverter
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
using System.Windows;
Expand All @@ -38,7 +39,7 @@ public partial class Application : System.Windows.Application
{
Major = 2,
Minor = 0,
Patch = 0,
Patch = 1,
};

private bool needToRunConversionThread;
Expand All @@ -47,7 +48,12 @@ public partial class Application : System.Windows.Application
private bool verbose;
private bool showSettings;
private bool showHelp;


[DllImport("kernel32.dll")]
static extern bool AttachConsole(uint dwProcessId);

const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;

public event EventHandler<ApplicationTerminateArgs> OnApplicationTerminate;

public static Version ApplicationVersion => Application.Version;
Expand All @@ -70,10 +76,18 @@ public void CancelAutoExit()
}
}

public static void AskForShutdown()
{
Application.Current.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown(Debug.FirstErrorCode)));
}

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

// Redirect standard output to the parent process in case the application is launch from command line.
AttachConsole(ATTACH_PARENT_PROCESS);

this.RegisterServices();

this.Initialize();
Expand Down Expand Up @@ -117,7 +131,7 @@ protected override void OnExit(ExitEventArgs e)

if (!this.isSessionEnding && upgradeService.UpgradeVersionDescription != null && upgradeService.UpgradeVersionDescription.NeedToUpgrade)
{
Debug.Log("A new version of file converter has been found: {0}.", upgradeService.UpgradeVersionDescription.LatestVersion);
Debug.Log($"A new version of file converter has been found: {upgradeService.UpgradeVersionDescription.LatestVersion}.");

if (string.IsNullOrEmpty(upgradeService.UpgradeVersionDescription.InstallerPath))
{
Expand All @@ -134,16 +148,16 @@ protected override void OnExit(ExitEventArgs e)
string installerPath = upgradeService.UpgradeVersionDescription.InstallerPath;
if (!System.IO.File.Exists(installerPath))
{
Debug.LogError("Can't find upgrade installer ({0}). Try to restart the application.", installerPath);
Debug.LogError($"Can't find upgrade installer ({installerPath}). Try to restart the application.");
return;
}

// Start process.
Debug.Log("Start file converter upgrade from version {0} to {1}.", ApplicationVersion, upgradeService.UpgradeVersionDescription.LatestVersion);
Debug.Log($"Start file converter upgrade from version {ApplicationVersion} to {upgradeService.UpgradeVersionDescription.LatestVersion}.");

ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(installerPath) { UseShellExecute = true, };

Debug.Log("Start upgrade process: {0}{1}.", System.IO.Path.GetFileName(startInfo.FileName), startInfo.Arguments);
Debug.Log($"Start upgrade process: {System.IO.Path.GetFileName(startInfo.FileName)}{startInfo.Arguments}.");
Process process = new System.Diagnostics.Process { StartInfo = startInfo };

process.Start();
Expand Down Expand Up @@ -172,7 +186,7 @@ private void RegisterServices()
else
{
Debug.LogError("Can't retrieve view model locator.");
this.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Application.AskForShutdown();
}

if (this.TryFindResource("Upgrade") is UpgradeService upgradeService)
Expand All @@ -182,7 +196,7 @@ private void RegisterServices()
else
{
Debug.LogError("Can't retrieve Upgrade service.");
this.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Application.AskForShutdown();
}

services
Expand Down Expand Up @@ -217,7 +231,7 @@ private void Initialize()
for (int index = 0; index < args.Length; index++)
{
string argument = args[index];
Debug.Log("Arg{0}: {1}", index, argument);
Debug.Log($"Arg{index}: {argument}");
}

Debug.Log(string.Empty);
Expand All @@ -233,7 +247,6 @@ private void Initialize()

// Parse arguments.
bool quitAfterStartup = false;
int quitExitCode = 0;
List<string> filePaths = new List<string>();
string conversionPresetName = null;
for (int index = 1; index < args.Length; index++)
Expand All @@ -254,8 +267,7 @@ private void Initialize()
case "post-install-init":
if (!settingsService.PostInstallationInitialization())
{
quitExitCode = 0x0F;
Debug.LogError(quitExitCode, $"Failed to execute post install initialization.");
Debug.LogError(errorCode: 0x0F, $"Failed to execute post install initialization.");
}

quitAfterStartup = true;
Expand All @@ -267,8 +279,7 @@ private void Initialize()

if (index >= args.Length - 1)
{
quitExitCode = 0x0B;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x0B, $"Invalid format.");
break;
}

Expand All @@ -277,8 +288,7 @@ private void Initialize()

if (!Helpers.RegisterShellExtension(shellExtensionPath))
{
quitExitCode = 0x0C;
Debug.LogError(quitExitCode, $"Failed to register shell extension {shellExtensionPath}.");
Debug.LogError(errorCode: 0x0C, $"Failed to register shell extension {shellExtensionPath}.");
}

break;
Expand All @@ -290,8 +300,7 @@ private void Initialize()

if (index >= args.Length - 1)
{
quitExitCode = 0x0D;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x0D, $"Invalid format.");
break;
}

Expand All @@ -300,8 +309,7 @@ private void Initialize()

if (!Helpers.UnregisterExtension(shellExtensionPath))
{
quitExitCode = 0x0E;
Debug.LogError(quitExitCode, $"Failed to unregister shell extension {shellExtensionPath}.");
Debug.LogError(errorCode: 0x0E, $"Failed to unregister shell extension {shellExtensionPath}.");
}

break;
Expand All @@ -320,8 +328,7 @@ private void Initialize()
if (index >= args.Length - 1)
{
quitAfterStartup = true;
quitExitCode = 0x01;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x01, $"Invalid format.");
break;
}

Expand All @@ -333,8 +340,7 @@ private void Initialize()
if (index >= args.Length - 1)
{
quitAfterStartup = true;
quitExitCode = 0x02;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x02, $"Invalid format.");
break;
}

Expand All @@ -353,8 +359,7 @@ private void Initialize()
catch (Exception exception)
{
quitAfterStartup = true;
quitExitCode = 0x03;
Debug.LogError(quitExitCode, $"Can't read input files list: {exception}");
Debug.LogError(errorCode: 0x03, $"Can't read input files list: {exception}");
}

index++;
Expand All @@ -368,7 +373,7 @@ private void Initialize()
break;

default:
Debug.LogError("Unknown application argument: '--{0}'.", parameterTitle);
Debug.LogError($"Unknown application argument: '--{parameterTitle}'.");
return;
}
}
Expand All @@ -380,18 +385,17 @@ private void Initialize()

if (settingsService.Settings == null)
{
Debug.LogError("Can't load File Converter settings. The application will now shutdown, if you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml.");
Debug.LogError(errorCode: 0x04, "Can't load File Converter settings. The application will now shutdown, if you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml.");
quitAfterStartup = true;
quitExitCode = 13;
}

if (quitAfterStartup)
{
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown(quitExitCode)));
Application.AskForShutdown();
return;
}

Debug.Assert(quitExitCode == 0, "An error happened during the initialization.");
Debug.Assert(Debug.FirstErrorCode == 0, "An error happened during the initialization.");

// Check for upgrade.
if (settingsService.Settings.CheckUpgradeAtStartup)
Expand All @@ -407,8 +411,8 @@ private void Initialize()
conversionPreset = settingsService.Settings.GetPresetFromName(conversionPresetName);
if (conversionPreset == null)
{
Debug.LogError("Invalid conversion preset '{0}'. (code 0x02)", conversionPresetName);
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Debug.LogError(errorCode: 0x02, $"Invalid conversion preset '{conversionPresetName}'.");
Application.AskForShutdown();
return;
}
}
Expand All @@ -418,7 +422,7 @@ private void Initialize()
IConversionService conversionService = Ioc.Default.GetRequiredService<IConversionService>();

// Create conversion jobs.
Debug.Log("Create jobs for conversion preset: '{0}'", conversionPreset.FullName);
Debug.Log($"Create jobs for conversion preset: '{conversionPreset.FullName}'");
try
{
for (int index = 0; index < filePaths.Count; index++)
Expand Down Expand Up @@ -488,7 +492,7 @@ private void ConversionService_ConversionJobsTerminated(object sender, Conversio
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(remainingTime));
}

this.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Application.AskForShutdown();
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions Application/FileConverter/ConversionJobs/ConversionJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void PrepareConversion(params string[] outputFilePaths)
this.State = ConversionState.Ready;
}

Debug.Log("Job initialized: Preset: '{0}' Input: {1} Output: {2}", this.ConversionPreset.FullName, this.InputFilePath, this.OutputFilePath);
Debug.Log($"Job initialized: Preset: '{this.ConversionPreset.FullName}' Input: {this.InputFilePath} Output: {this.OutputFilePath}");

if (this.State != ConversionState.Failed)
{
Expand All @@ -332,7 +332,7 @@ public void StartConversion()
throw new Exception("Invalid conversion state.");
}

Debug.Log("Convert file {0} to {1}.", this.InputFilePath, this.OutputFilePath);
Debug.Log($"Convert file {this.InputFilePath} to {this.OutputFilePath}.");

this.StartTime = DateTime.Now;
this.State = ConversionState.InProgress;
Expand Down Expand Up @@ -407,8 +407,8 @@ protected virtual void OnConversionFailed()
}
catch (Exception exception)
{
Debug.Log("Can't delete file '{0}' after conversion job failure.", outputFilePath);
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
Debug.Log($"Can't delete file '{outputFilePath}' after conversion job failure.");
Debug.Log($"An exception as been thrown: {exception}.");
}
}
}
Expand Down Expand Up @@ -436,12 +436,12 @@ protected virtual void OnConversionSucceed()

string newPath = PathHelpers.GenerateUniquePath(archivePath + "\\" + inputFilename);
System.IO.File.Move(this.InputFilePath, newPath);
Debug.Log("Input file moved in archive folder: '{0}'", newPath);
Debug.Log($"Input file moved in archive folder: '{newPath}'");
break;

case InputPostConversionAction.Delete:
System.IO.File.Delete(this.InputFilePath);
Debug.Log("Input file deleted: '{0}'", this.initialInputPath);
Debug.Log($"Input file deleted: '{this.initialInputPath}'");
break;
}

Expand All @@ -455,7 +455,7 @@ protected virtual void OnConversionSucceed()

protected void ConversionFailed(string exitingMessage)
{
Debug.Log("Fail: {0}", exitingMessage);
Debug.Log($"Fail: {exitingMessage}");

if (this.State == ConversionState.Failed)
{
Expand Down Expand Up @@ -483,7 +483,7 @@ private void ChangeOutputFileTimestampToMatchOriginal()
var originalFileCreationTime = System.IO.File.GetCreationTimeUtc(this.InputFilePath);
var originalFileLastAccesTime = System.IO.File.GetLastAccessTimeUtc(this.InputFilePath);
var originalFileLastWriteTime = System.IO.File.GetLastWriteTimeUtc(this.InputFilePath);
Debug.Log(" original timestamp: {0}, {1}, {2}", originalFileCreationTime, originalFileLastAccesTime, originalFileLastWriteTime);
Debug.Log($" original timestamp: {originalFileCreationTime}, {originalFileLastAccesTime}, {originalFileLastWriteTime}");

for (int index = 0; index < this.OutputFilePaths.Length; index++)
{
Expand All @@ -493,12 +493,12 @@ private void ChangeOutputFileTimestampToMatchOriginal()
System.IO.File.SetCreationTimeUtc(outputFilePath, originalFileCreationTime);
System.IO.File.SetLastAccessTimeUtc(outputFilePath, originalFileLastAccesTime);
System.IO.File.SetLastWriteTimeUtc(outputFilePath, originalFileLastWriteTime);
Debug.Log(" output file '{0}' timestamp changed", outputFilePath);
Debug.Log($" output file '{outputFilePath}' timestamp changed");
}
catch (Exception exception)
{
Debug.Log("Can't change timestamp from file '{0}'", outputFilePath);
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
Debug.Log($"Can't change timestamp from file '{outputFilePath}'");
Debug.Log($"An exception as been thrown: {exception}.");
}
}

Expand Down
10 changes: 5 additions & 5 deletions Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ protected override void Convert()

this.UserState = Properties.Resources.ConversionStateConversion;

Diagnostics.Debug.Log("Convert excel document to pdf.");
Debug.Log("Convert excel document to pdf.");
this.document.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, this.intermediateFilePath);

Diagnostics.Debug.Log("Close excel document '{0}'.", this.InputFilePath);
Debug.Log($"Close excel document '{this.InputFilePath}'.");
this.document.Close(false);
this.document = null;

Expand All @@ -126,7 +126,7 @@ protected override void Convert()

Task updateProgress = this.UpdateProgress();

Diagnostics.Debug.Log("Convert pdf to images.");
Debug.Log("Convert pdf to images.");

this.pdf2ImageConversionJob.StartConversion();

Expand All @@ -138,7 +138,7 @@ protected override void Convert()

if (!string.IsNullOrEmpty(this.intermediateFilePath))
{
Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");

File.Delete(this.intermediateFilePath);
}
Expand Down Expand Up @@ -213,7 +213,7 @@ private bool TryLoadDocumentIfNecessary()

if (this.document == null)
{
Diagnostics.Debug.Log("Load excel document '{0}'.", this.InputFilePath);
Debug.Log($"Load excel document '{this.InputFilePath}'.");

this.document = this.application.Workbooks.Open(this.InputFilePath, System.Reflection.Missing.Value, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected override void Convert()
return;
}

Debug.Log("CDA extracted to {0}.", this.intermediateFilePath);
Debug.Log($"CDA extracted to {this.intermediateFilePath}.");
Debug.Log(string.Empty);
Debug.Log("Start compression.");

Expand All @@ -188,7 +188,7 @@ protected override void Convert()
}

Debug.Log(string.Empty);
Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");

File.Delete(this.intermediateFilePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static string ComputeTransformArgs(ConversionPreset conversionPreset)
}
else
{
Diagnostics.Debug.LogError("Unsupported rotation: {0", rotationAngleInDegrees);
Diagnostics.Debug.LogError($"Unsupported rotation: {rotationAngleInDegrees}°");
}
}

Expand Down
Loading

0 comments on commit 58228da

Please sign in to comment.