Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tichau committed Apr 24, 2016
2 parents 37f976f + 22655e5 commit a6baaba
Show file tree
Hide file tree
Showing 42 changed files with 1,429 additions and 363 deletions.
61 changes: 51 additions & 10 deletions Application/FileConverter/Application.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public partial class Application : System.Windows.Application
private static readonly Version Version = new Version()
{
Major = 0,
Minor = 6,
Minor = 7,
Patch = 0,
};

Expand All @@ -41,13 +41,16 @@ public partial class Application : System.Windows.Application

private bool needToRunConversionThread;
private bool cancelAutoExit;
private bool isSessionEnding;
private UpgradeVersionDescription upgradeVersionDescription = null;

public Application()
{
this.ConvertionJobs = this.conversionJobs.AsReadOnly();
}

public event EventHandler<ApplicationTerminateArgs> OnApplicationTerminate;

public static Version ApplicationVersion
{
get
Expand Down Expand Up @@ -89,6 +92,11 @@ public bool Verbose
public void CancelAutoExit()
{
this.cancelAutoExit = true;

if (this.OnApplicationTerminate != null)
{
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(float.NaN));
}
}

protected override void OnStartup(StartupEventArgs e)
Expand All @@ -109,8 +117,8 @@ protected override void OnExit(ExitEventArgs e)
base.OnExit(e);

Debug.Log("Exit application.");

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

Expand Down Expand Up @@ -154,11 +162,19 @@ protected override void OnExit(ExitEventArgs e)
Debug.Release();
}

protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);

this.isSessionEnding = true;
this.Shutdown();
}

private void Initialize()
{
Diagnostics.Debug.Log("File Converter v" + ApplicationVersion.ToString());
Diagnostics.Debug.Log("The number of processors on this computer is {0}. Set the default number of conversion threads to {0}", Environment.ProcessorCount);
this.numberOfConversionThread = Environment.ProcessorCount;
this.numberOfConversionThread = System.Math.Max(1, Environment.ProcessorCount / 2);
Diagnostics.Debug.Log("The number of processors on this computer is {0}. Set the default number of conversion threads to {0}", this.numberOfConversionThread);

// Retrieve arguments.
Debug.Log("Retrieve arguments...");
Expand Down Expand Up @@ -198,6 +214,11 @@ private void Initialize()
for (int index = 1; index < args.Length; index++)
{
string argument = args[index];
if (string.IsNullOrEmpty(argument))
{
continue;
}

if (argument.StartsWith("--"))
{
// This is an optional parameter.
Expand Down Expand Up @@ -381,13 +402,33 @@ private void ConvertFiles()
allConversionsSucceed &= this.conversionJobs[index].State == ConversionJob.ConversionState.Done;
}

if (this.cancelAutoExit)
{
return;
}

if (allConversionsSucceed)
{
System.Threading.Thread.Sleep((int)this.Settings.DurationBetweenEndOfConversionsAndApplicationExit * 1000);
float remainingTime = this.Settings.DurationBetweenEndOfConversionsAndApplicationExit;
while (remainingTime > 0f)
{
if (this.OnApplicationTerminate != null)
{
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(remainingTime));
}

System.Threading.Thread.Sleep(1000);
remainingTime--;

if (this.cancelAutoExit)
if (this.cancelAutoExit)
{
return;
}
}

if (this.OnApplicationTerminate != null)
{
return;
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(remainingTime));
}

Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Expand All @@ -413,9 +454,9 @@ private void ExecuteConversionJob(object parameter)
{
conversionJob.StartConvertion();
}
catch (Exception)
catch (Exception exception)
{
throw;
Debug.LogError("Failure during conversion: {0}", exception.ToString());
}

if (conversionJob.State == ConversionJob.ConversionState.Done && !System.IO.File.Exists(conversionJob.OutputFilePath))
Expand Down
18 changes: 18 additions & 0 deletions Application/FileConverter/ApplicationTerminateArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <copyright file="ApplicationTerminateArgs.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>

namespace FileConverter
{
public class ApplicationTerminateArgs : System.EventArgs
{
public ApplicationTerminateArgs(float remainingTimeBeforeTermination)
{
this.RemainingTimeBeforeTermination = remainingTimeBeforeTermination;
}

public float RemainingTimeBeforeTermination
{
get;
private set;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="CancelConversionJobCommand.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>

namespace FileConverter.ConversionJobs
{
using System;
using System.Windows.Input;

public class CancelConversionJobCommand : ICommand
{
private readonly ConversionJob conversionJob;

public CancelConversionJobCommand(ConversionJob conversionJob)
{
this.conversionJob = conversionJob;
}

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
if (this.conversionJob == null)
{
return false;
}

return this.conversionJob.IsCancelable && this.conversionJob.State == ConversionJob.ConversionState.InProgress;
}

public void Execute(object parameter)
{
this.conversionJob?.Cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public enum ConversionFlags
{
None = 0x00,

CdaExtraction = 0x01,
CdDriveExtraction = 0x01,
}
}
61 changes: 57 additions & 4 deletions Application/FileConverter/ConversionJobs/ConversionJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class ConversionJob : INotifyPropertyChanged
private string errorMessage = string.Empty;
private string initialInputPath = string.Empty;
private string userState = string.Empty;
private CancelConversionJobCommand cancelCommand;

public ConversionJob()
{
Expand Down Expand Up @@ -126,6 +127,31 @@ public ConversionFlags StateFlags
protected set;
}

public CancelConversionJobCommand CancelCommand
{
get
{
if (this.cancelCommand == null)
{
this.cancelCommand = new CancelConversionJobCommand(this);
}

return this.cancelCommand;
}
}

public bool IsCancelable
{
get;
protected set;
}

protected bool CancelIsRequested
{
get;
private set;
}

protected virtual InputPostConversionAction InputPostConversionAction
{
get
Expand All @@ -141,7 +167,7 @@ protected virtual InputPostConversionAction InputPostConversionAction

public virtual bool CanStartConversion(ConversionFlags conversionFlags)
{
return true;
return (conversionFlags & ConversionFlags.CdDriveExtraction) == 0;
}

public void PrepareConversion(string inputFilePath, string outputFilePath = null)
Expand Down Expand Up @@ -225,6 +251,12 @@ public void PrepareConversion(string inputFilePath, string outputFilePath = null
return;
}

// Check if the input file is located on a cd drive.
if (PathHelpers.IsOnCDDrive(this.InputFilePath))
{
this.StateFlags = ConversionFlags.CdDriveExtraction;
}

this.Initialize();

if (this.State == ConversionState.Unknown)
Expand Down Expand Up @@ -252,7 +284,7 @@ public void StartConvertion()
Debug.Log("Convert file {0} to {1}.", this.InputFilePath, this.OutputFilePath);

this.State = ConversionState.InProgress;

try
{
this.Convert();
Expand All @@ -262,6 +294,8 @@ public void StartConvertion()
this.ConversionFailed(exception.Message);
}

this.StateFlags = ConversionFlags.None;

if (this.State == ConversionState.Failed)
{
this.OnConversionFailed();
Expand All @@ -272,6 +306,17 @@ public void StartConvertion()
}
}

public virtual void Cancel()
{
if (!this.IsCancelable || this.State != ConversionState.InProgress)
{
return;
}

this.CancelIsRequested = true;
this.ConversionFailed("Canceled.");
}

protected virtual void Convert()
{
}
Expand All @@ -284,9 +329,17 @@ protected virtual void OnConversionFailed()
{
Debug.Log("Conversion Failed.");

if (System.IO.File.Exists(this.OutputFilePath))
try
{
if (System.IO.File.Exists(this.OutputFilePath))
{
System.IO.File.Delete(this.OutputFilePath);
}
}
catch (Exception exception)
{
System.IO.File.Delete(this.OutputFilePath);
Debug.Log("Can't delete file '{0}' after conversion job failure.", this.OutputFilePath);
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public static ConversionJob Create(ConversionPreset conversionPreset, string inp
return new ConversionJob_Ico(conversionPreset);
}

if (conversionPreset.OutputType == OutputType.Gif)
{
return new ConversionJob_Gif(conversionPreset);
}

if (PathHelpers.GetExtensionCategory(extension) == PathHelpers.InputCategoryNames.Image)
{
return new ConversionJob_ImageMagick(conversionPreset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ protected override InputPostConversionAction InputPostConversionAction
return InputPostConversionAction.None;
}
}

public override bool CanStartConversion(ConversionFlags conversionFlags)
{
return (conversionFlags & ConversionFlags.CdaExtraction) == 0;
}


protected override void Initialize()
{
base.Initialize();
Expand Down Expand Up @@ -108,8 +103,6 @@ protected override void Initialize()
this.compressionConversionJob = ConversionJobFactory.Create(this.ConversionPreset, this.intermediateFilePath);
this.compressionConversionJob.PrepareConversion(this.intermediateFilePath, this.OutputFilePath);
this.compressionThread = new Thread(this.CompressAsync);

this.StateFlags = ConversionFlags.CdaExtraction;
}

protected override void Convert()
Expand Down
Loading

0 comments on commit a6baaba

Please sign in to comment.