Skip to content

Commit a6baaba

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/development'
2 parents 37f976f + 22655e5 commit a6baaba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1429
-363
lines changed

Application/FileConverter/Application.xaml.cs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public partial class Application : System.Windows.Application
3131
private static readonly Version Version = new Version()
3232
{
3333
Major = 0,
34-
Minor = 6,
34+
Minor = 7,
3535
Patch = 0,
3636
};
3737

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

4242
private bool needToRunConversionThread;
4343
private bool cancelAutoExit;
44+
private bool isSessionEnding;
4445
private UpgradeVersionDescription upgradeVersionDescription = null;
4546

4647
public Application()
4748
{
4849
this.ConvertionJobs = this.conversionJobs.AsReadOnly();
4950
}
5051

52+
public event EventHandler<ApplicationTerminateArgs> OnApplicationTerminate;
53+
5154
public static Version ApplicationVersion
5255
{
5356
get
@@ -89,6 +92,11 @@ public bool Verbose
8992
public void CancelAutoExit()
9093
{
9194
this.cancelAutoExit = true;
95+
96+
if (this.OnApplicationTerminate != null)
97+
{
98+
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(float.NaN));
99+
}
92100
}
93101

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

111119
Debug.Log("Exit application.");
112-
113-
if (this.upgradeVersionDescription != null && this.upgradeVersionDescription.NeedToUpgrade)
120+
121+
if (!this.isSessionEnding && this.upgradeVersionDescription != null && this.upgradeVersionDescription.NeedToUpgrade)
114122
{
115123
Debug.Log("A new version of file converter has been found: {0}.", this.upgradeVersionDescription.LatestVersion);
116124

@@ -154,11 +162,19 @@ protected override void OnExit(ExitEventArgs e)
154162
Debug.Release();
155163
}
156164

165+
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
166+
{
167+
base.OnSessionEnding(e);
168+
169+
this.isSessionEnding = true;
170+
this.Shutdown();
171+
}
172+
157173
private void Initialize()
158174
{
159175
Diagnostics.Debug.Log("File Converter v" + ApplicationVersion.ToString());
160-
Diagnostics.Debug.Log("The number of processors on this computer is {0}. Set the default number of conversion threads to {0}", Environment.ProcessorCount);
161-
this.numberOfConversionThread = Environment.ProcessorCount;
176+
this.numberOfConversionThread = System.Math.Max(1, Environment.ProcessorCount / 2);
177+
Diagnostics.Debug.Log("The number of processors on this computer is {0}. Set the default number of conversion threads to {0}", this.numberOfConversionThread);
162178

163179
// Retrieve arguments.
164180
Debug.Log("Retrieve arguments...");
@@ -198,6 +214,11 @@ private void Initialize()
198214
for (int index = 1; index < args.Length; index++)
199215
{
200216
string argument = args[index];
217+
if (string.IsNullOrEmpty(argument))
218+
{
219+
continue;
220+
}
221+
201222
if (argument.StartsWith("--"))
202223
{
203224
// This is an optional parameter.
@@ -381,13 +402,33 @@ private void ConvertFiles()
381402
allConversionsSucceed &= this.conversionJobs[index].State == ConversionJob.ConversionState.Done;
382403
}
383404

405+
if (this.cancelAutoExit)
406+
{
407+
return;
408+
}
409+
384410
if (allConversionsSucceed)
385411
{
386-
System.Threading.Thread.Sleep((int)this.Settings.DurationBetweenEndOfConversionsAndApplicationExit * 1000);
412+
float remainingTime = this.Settings.DurationBetweenEndOfConversionsAndApplicationExit;
413+
while (remainingTime > 0f)
414+
{
415+
if (this.OnApplicationTerminate != null)
416+
{
417+
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(remainingTime));
418+
}
419+
420+
System.Threading.Thread.Sleep(1000);
421+
remainingTime--;
387422

388-
if (this.cancelAutoExit)
423+
if (this.cancelAutoExit)
424+
{
425+
return;
426+
}
427+
}
428+
429+
if (this.OnApplicationTerminate != null)
389430
{
390-
return;
431+
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(remainingTime));
391432
}
392433

393434
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
@@ -413,9 +454,9 @@ private void ExecuteConversionJob(object parameter)
413454
{
414455
conversionJob.StartConvertion();
415456
}
416-
catch (Exception)
457+
catch (Exception exception)
417458
{
418-
throw;
459+
Debug.LogError("Failure during conversion: {0}", exception.ToString());
419460
}
420461

421462
if (conversionJob.State == ConversionJob.ConversionState.Done && !System.IO.File.Exists(conversionJob.OutputFilePath))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// <copyright file="ApplicationTerminateArgs.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
2+
3+
namespace FileConverter
4+
{
5+
public class ApplicationTerminateArgs : System.EventArgs
6+
{
7+
public ApplicationTerminateArgs(float remainingTimeBeforeTermination)
8+
{
9+
this.RemainingTimeBeforeTermination = remainingTimeBeforeTermination;
10+
}
11+
12+
public float RemainingTimeBeforeTermination
13+
{
14+
get;
15+
private set;
16+
}
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// <copyright file="CancelConversionJobCommand.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
2+
3+
namespace FileConverter.ConversionJobs
4+
{
5+
using System;
6+
using System.Windows.Input;
7+
8+
public class CancelConversionJobCommand : ICommand
9+
{
10+
private readonly ConversionJob conversionJob;
11+
12+
public CancelConversionJobCommand(ConversionJob conversionJob)
13+
{
14+
this.conversionJob = conversionJob;
15+
}
16+
17+
public event EventHandler CanExecuteChanged;
18+
19+
public bool CanExecute(object parameter)
20+
{
21+
if (this.conversionJob == null)
22+
{
23+
return false;
24+
}
25+
26+
return this.conversionJob.IsCancelable && this.conversionJob.State == ConversionJob.ConversionState.InProgress;
27+
}
28+
29+
public void Execute(object parameter)
30+
{
31+
this.conversionJob?.Cancel();
32+
}
33+
}
34+
}

Application/FileConverter/ConversionJobs/ConversionFlags.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public enum ConversionFlags
99
{
1010
None = 0x00,
1111

12-
CdaExtraction = 0x01,
12+
CdDriveExtraction = 0x01,
1313
}
1414
}

Application/FileConverter/ConversionJobs/ConversionJob.cs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class ConversionJob : INotifyPropertyChanged
1616
private string errorMessage = string.Empty;
1717
private string initialInputPath = string.Empty;
1818
private string userState = string.Empty;
19+
private CancelConversionJobCommand cancelCommand;
1920

2021
public ConversionJob()
2122
{
@@ -126,6 +127,31 @@ public ConversionFlags StateFlags
126127
protected set;
127128
}
128129

130+
public CancelConversionJobCommand CancelCommand
131+
{
132+
get
133+
{
134+
if (this.cancelCommand == null)
135+
{
136+
this.cancelCommand = new CancelConversionJobCommand(this);
137+
}
138+
139+
return this.cancelCommand;
140+
}
141+
}
142+
143+
public bool IsCancelable
144+
{
145+
get;
146+
protected set;
147+
}
148+
149+
protected bool CancelIsRequested
150+
{
151+
get;
152+
private set;
153+
}
154+
129155
protected virtual InputPostConversionAction InputPostConversionAction
130156
{
131157
get
@@ -141,7 +167,7 @@ protected virtual InputPostConversionAction InputPostConversionAction
141167

142168
public virtual bool CanStartConversion(ConversionFlags conversionFlags)
143169
{
144-
return true;
170+
return (conversionFlags & ConversionFlags.CdDriveExtraction) == 0;
145171
}
146172

147173
public void PrepareConversion(string inputFilePath, string outputFilePath = null)
@@ -225,6 +251,12 @@ public void PrepareConversion(string inputFilePath, string outputFilePath = null
225251
return;
226252
}
227253

254+
// Check if the input file is located on a cd drive.
255+
if (PathHelpers.IsOnCDDrive(this.InputFilePath))
256+
{
257+
this.StateFlags = ConversionFlags.CdDriveExtraction;
258+
}
259+
228260
this.Initialize();
229261

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

254286
this.State = ConversionState.InProgress;
255-
287+
256288
try
257289
{
258290
this.Convert();
@@ -262,6 +294,8 @@ public void StartConvertion()
262294
this.ConversionFailed(exception.Message);
263295
}
264296

297+
this.StateFlags = ConversionFlags.None;
298+
265299
if (this.State == ConversionState.Failed)
266300
{
267301
this.OnConversionFailed();
@@ -272,6 +306,17 @@ public void StartConvertion()
272306
}
273307
}
274308

309+
public virtual void Cancel()
310+
{
311+
if (!this.IsCancelable || this.State != ConversionState.InProgress)
312+
{
313+
return;
314+
}
315+
316+
this.CancelIsRequested = true;
317+
this.ConversionFailed("Canceled.");
318+
}
319+
275320
protected virtual void Convert()
276321
{
277322
}
@@ -284,9 +329,17 @@ protected virtual void OnConversionFailed()
284329
{
285330
Debug.Log("Conversion Failed.");
286331

287-
if (System.IO.File.Exists(this.OutputFilePath))
332+
try
333+
{
334+
if (System.IO.File.Exists(this.OutputFilePath))
335+
{
336+
System.IO.File.Delete(this.OutputFilePath);
337+
}
338+
}
339+
catch (Exception exception)
288340
{
289-
System.IO.File.Delete(this.OutputFilePath);
341+
Debug.Log("Can't delete file '{0}' after conversion job failure.", this.OutputFilePath);
342+
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
290343
}
291344
}
292345

Application/FileConverter/ConversionJobs/ConversionJobFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public static ConversionJob Create(ConversionPreset conversionPreset, string inp
1818
return new ConversionJob_Ico(conversionPreset);
1919
}
2020

21+
if (conversionPreset.OutputType == OutputType.Gif)
22+
{
23+
return new ConversionJob_Gif(conversionPreset);
24+
}
25+
2126
if (PathHelpers.GetExtensionCategory(extension) == PathHelpers.InputCategoryNames.Image)
2227
{
2328
return new ConversionJob_ImageMagick(conversionPreset);

Application/FileConverter/ConversionJobs/ConversionJob_ExtractCDA.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ protected override InputPostConversionAction InputPostConversionAction
3535
return InputPostConversionAction.None;
3636
}
3737
}
38-
39-
public override bool CanStartConversion(ConversionFlags conversionFlags)
40-
{
41-
return (conversionFlags & ConversionFlags.CdaExtraction) == 0;
42-
}
43-
38+
4439
protected override void Initialize()
4540
{
4641
base.Initialize();
@@ -108,8 +103,6 @@ protected override void Initialize()
108103
this.compressionConversionJob = ConversionJobFactory.Create(this.ConversionPreset, this.intermediateFilePath);
109104
this.compressionConversionJob.PrepareConversion(this.intermediateFilePath, this.OutputFilePath);
110105
this.compressionThread = new Thread(this.CompressAsync);
111-
112-
this.StateFlags = ConversionFlags.CdaExtraction;
113106
}
114107

115108
protected override void Convert()

0 commit comments

Comments
 (0)