Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

could you add the following? // se podría agregar lo siguiente? #315

Open
wants to merge 6 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
495 changes: 272 additions & 223 deletions Application/FileConverter/Application.xaml.cs

Large diffs are not rendered by default.

155 changes: 64 additions & 91 deletions Application/FileConverter/ConversionJobs/ConversionJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,31 @@ namespace FileConverter.ConversionJobs
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using System.Text;

using FileConverter.Commands;
using FileConverter.Diagnostics;

using GalaSoft.MvvmLight.Command;

public class ConversionJob : INotifyPropertyChanged
{
private float progress = 0f;
private DateTime startTime;
private ConversionState state = ConversionState.Unknown;
private string errorMessage = string.Empty;
private string userState = string.Empty;
private RelayCommand cancelCommand;
private CancelConversionJobCommand cancelCommand;

private readonly string initialInputPath;
private int currentOutputFilePathIndex;
private string initialInputPath = string.Empty;
private int currentOuputFilePathIndex;

public ConversionJob()
{
this.State = ConversionState.InProgress;
this.State = ConversionState.Unknown;
this.ConversionPreset = null;
this.initialInputPath = string.Empty;
this.InputFilePath = "C:\\My file.png";
this.UserState = "Design Mode";
this.InputFilePath = string.Empty;
}

public ConversionJob(ConversionPreset conversionPreset, string inputFilePath)
public ConversionJob(ConversionPreset conversionPreset, string inputFilePath) : this()
{
if (conversionPreset == null)
{
Expand All @@ -44,15 +41,24 @@ public ConversionJob(ConversionPreset conversionPreset, string inputFilePath)
throw new ArgumentNullException(nameof(inputFilePath));
}

this.State = ConversionState.Unknown;
this.initialInputPath = inputFilePath;
this.InputFilePath = inputFilePath;
this.ConversionPreset = conversionPreset;
this.UserState = Properties.Resources.ConversionStatePrepareConversion;
}

public event PropertyChangedEventHandler PropertyChanged;


public enum ConversionState
{
Unknown,

Ready,
InProgress,
Done,
Failed,
}

public ConversionPreset ConversionPreset
{
get;
Expand All @@ -62,7 +68,7 @@ public ConversionPreset ConversionPreset
public string InputFilePath
{
get;
set;
private set;
}

public string OutputFilePath
Expand All @@ -74,23 +80,26 @@ public string OutputFilePath
return string.Empty;
}

if (this.CurrentOutputFilePathIndex < 0)
if (this.CurrentOuputFilePathIndex < 0)
{
return this.OutputFilePaths[0];
}

if (this.CurrentOutputFilePathIndex >= this.OutputFilePaths.Length)
if (this.CurrentOuputFilePathIndex >= this.OutputFilePaths.Length)
{
return this.OutputFilePaths[this.OutputFilePaths.Length - 1];
}

return this.OutputFilePaths[this.CurrentOutputFilePathIndex];
return this.OutputFilePaths[this.CurrentOuputFilePathIndex];
}
}

public ConversionState State
{
get => this.state;
get
{
return this.state;
}

private set
{
Expand All @@ -101,7 +110,10 @@ private set

public string UserState
{
get => this.userState;
get
{
return this.userState;
}

protected set
{
Expand All @@ -112,29 +124,24 @@ protected set

public float Progress
{
get => this.progress;

protected set
get
{
this.progress = value;
this.NotifyPropertyChanged();
return this.progress;
}
}

public DateTime StartTime
{
get => this.startTime;

protected set
{
this.startTime = value;
this.progress = value;
this.NotifyPropertyChanged();
}
}

public string ErrorMessage
{
get => this.errorMessage;
get
{
return this.errorMessage;
}

private set
{
Expand All @@ -149,32 +156,41 @@ public ConversionFlags StateFlags
protected set;
}

public ICommand CancelCommand
public CancelConversionJobCommand CancelCommand
{
get
{
if (this.cancelCommand == null)
{
this.cancelCommand = new RelayCommand(this.Cancel, this.IsCancelable);
this.cancelCommand = new CancelConversionJobCommand(this);
}

return this.cancelCommand;
}
}

public bool IsCancelable
{
get;
protected set;
}

protected bool CancelIsRequested
{
get;
private set;
}

protected int CurrentOutputFilePathIndex
protected int CurrentOuputFilePathIndex
{
get => this.currentOutputFilePathIndex;
get
{
return this.currentOuputFilePathIndex;
}

set
{
this.currentOutputFilePathIndex = value;
this.currentOuputFilePathIndex = value;
this.NotifyPropertyChanged(nameof(this.OutputFilePath));
}
}
Expand All @@ -192,8 +208,6 @@ protected virtual InputPostConversionAction InputPostConversionAction
}
}

protected virtual bool IsCancelable => this.State == ConversionState.InProgress;

protected string[] OutputFilePaths
{
get;
Expand Down Expand Up @@ -226,7 +240,7 @@ public void PrepareConversion(params string[] outputFilePaths)
this.OutputFilePaths = outputFilePaths;
if (this.OutputFilePaths.Length == 0)
{
int outputFilesCount = this.GetOutputFilesCount();
int outputFilesCount = this.GetOuputFilesCount();
this.OutputFilePaths = new string[outputFilesCount];
}

Expand Down Expand Up @@ -282,39 +296,30 @@ public void PrepareConversion(params string[] outputFilePaths)
this.OutputFilePaths[index] = path;
}

this.CurrentOutputFilePathIndex = 0;
this.CurrentOuputFilePathIndex = 0;

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

try
{
this.Initialize();
}
catch (Exception exception)
{
this.ConversionFailed(Properties.Resources.ErrorDuringJobInitialization);
Debug.Log(exception.ToString());
return;
}
this.Initialize();

if (this.State == ConversionState.Unknown)
{
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: '{0}' Input: {1} Output: {2}", this.ConversionPreset.Name, this.InputFilePath, this.OutputFilePath);

if (this.State != ConversionState.Failed)
{
this.UserState = Properties.Resources.ConversionStateInQueue;
}
}

public void StartConversion()
public void StartConvertion()
{
if (this.ConversionPreset == null)
{
Expand All @@ -328,7 +333,6 @@ public void StartConversion()

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

this.StartTime = DateTime.Now;
this.State = ConversionState.InProgress;

try
Expand All @@ -351,19 +355,19 @@ public void StartConversion()
this.OnConversionSucceed();
}

if (this.State == ConversionState.Done && !this.AllOutputFilesExists())
if (this.State == ConversionJob.ConversionState.Done && !this.AllOuputFilesExists())
{
Debug.LogError(Properties.Resources.ErrorCantFindOutputFiles);
}
else if (this.State == ConversionState.Failed && this.AtLeastOneOutputFilesExists())
else if (this.State == ConversionJob.ConversionState.Failed && this.AtLeastOneOuputFilesExists())
{
Debug.Log(Properties.Resources.ErrorConversionFailedWithOutput);
}
}

public virtual void Cancel()
{
if (!this.IsCancelable)
if (!this.IsCancelable || this.State != ConversionState.InProgress)
{
return;
}
Expand All @@ -372,7 +376,7 @@ public virtual void Cancel()
this.ConversionFailed(Properties.Resources.ErrorCanceled);
}

protected virtual int GetOutputFilesCount()
protected virtual int GetOuputFilesCount()
{
return 1;
}
Expand Down Expand Up @@ -411,8 +415,6 @@ protected virtual void OnConversionSucceed()
{
Debug.Log("Conversion Succeed!");

this.ChangeOutputFileTimestampToMatchOriginal();

// Apply the input post conversion action.
switch (this.InputPostConversionAction)
{
Expand Down Expand Up @@ -454,7 +456,7 @@ protected void ConversionFailed(string exitingMessage)
if (this.State == ConversionState.Failed)
{
// Already failed, don't override informations.
return;
return;
}

this.State = ConversionState.Failed;
Expand All @@ -470,36 +472,7 @@ protected void NotifyPropertyChanged([CallerMemberName] string propertyName = ""
}
}

private void ChangeOutputFileTimestampToMatchOriginal()
{
Debug.Log("Changing output files timestamp to match original timestamp ...");

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);

for (int index = 0; index < this.OutputFilePaths.Length; index++)
{
string outputFilePath = this.OutputFilePaths[index];
try
{
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);
}
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("... timestamp matching finished.");
}

private bool AllOutputFilesExists()
private bool AllOuputFilesExists()
{
for (int index = 0; index < this.OutputFilePaths.Length; index++)
{
Expand All @@ -513,7 +486,7 @@ private bool AllOutputFilesExists()
return true;
}

private bool AtLeastOneOutputFilesExists()
private bool AtLeastOneOuputFilesExists()
{
for (int index = 0; index < this.OutputFilePaths.Length; index++)
{
Expand Down
Loading