Clean code

Clean code
This commit is contained in:
Adrien Allard committed 2020-11-16 21:52:28 +01:00
1 parent 7be821bbdc
commit 5e7761938c
30 files changed
+77 -171

No files matched your search

@@ -20,7 +20,7 @@ namespace FileConverter.ConversionJobs
private string userState = string.Empty;
private RelayCommand cancelCommand;
private string initialInputPath;
private readonly string initialInputPath;
private int currentOutputFilePathIndex;
public ConversionJob()
@@ -101,10 +101,7 @@ namespace FileConverter.ConversionJobs
public string UserState
{
get
{
return this.userState;
}
get => this.userState;
protected set
{
@@ -115,10 +112,7 @@ namespace FileConverter.ConversionJobs
public float Progress
{
get
{
return this.progress;
}
get => this.progress;
protected set
{
@@ -127,12 +121,9 @@ namespace FileConverter.ConversionJobs
}
}
public System.DateTime StartTime
public DateTime StartTime
{
get
{
return this.startTime;
}
get => this.startTime;
protected set
{
@@ -143,10 +134,7 @@ namespace FileConverter.ConversionJobs
public string ErrorMessage
{
get
{
return this.errorMessage;
}
get => this.errorMessage;
private set
{
@@ -182,10 +170,7 @@ namespace FileConverter.ConversionJobs
protected int CurrentOutputFilePathIndex
{
get
{
return this.currentOutputFilePathIndex;
}
get => this.currentOutputFilePathIndex;
set
{
@@ -26,13 +26,7 @@ namespace FileConverter.ConversionJobs
{
}
protected override ApplicationName Application
{
get
{
return ApplicationName.Excel;
}
}
protected override ApplicationName Application => ApplicationName.Excel;
protected override bool IsCancelable => false;
@@ -16,7 +16,7 @@ namespace FileConverter.ConversionJobs
return string.Empty;
}
return string.Format("{0} \"{1}\"", optionName, args);
return $"{optionName} \"{args}\"";
}
/// <summary>
@@ -21,7 +21,7 @@ namespace FileConverter.ConversionJobs
private ProcessStartInfo ffmpegProcessStartInfo;
private List<FFMpegPass> ffmpegArgumentStringByPass = new List<FFMpegPass>();
private readonly List<FFMpegPass> ffmpegArgumentStringByPass = new List<FFMpegPass>();
public ConversionJob_FFMPEG() : base()
{
@@ -72,12 +72,13 @@ namespace FileConverter.ConversionJobs
return;
}
this.ffmpegProcessStartInfo = new ProcessStartInfo(ffmpegPath);
this.ffmpegProcessStartInfo.CreateNoWindow = true;
this.ffmpegProcessStartInfo.UseShellExecute = false;
this.ffmpegProcessStartInfo.RedirectStandardOutput = true;
this.ffmpegProcessStartInfo.RedirectStandardError = true;
this.ffmpegProcessStartInfo = new ProcessStartInfo(ffmpegPath)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
this.FillFFMpegArgumentsList();
}
@@ -27,13 +27,7 @@ namespace FileConverter.ConversionJobs
{
}
protected override ApplicationName Application
{
get
{
return ApplicationName.PowerPoint;
}
}
protected override ApplicationName Application => ApplicationName.PowerPoint;
protected override bool IsCancelable => false;
@@ -26,13 +26,7 @@ namespace FileConverter.ConversionJobs
{
}
protected override ApplicationName Application
{
get
{
return ApplicationName.Word;
}
}
protected override ApplicationName Application => ApplicationName.Word;
protected override bool IsCancelable => false;
@@ -4,16 +4,12 @@ namespace FileConverter
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Xml.Serialization;
using FileConverter.Controls;
using FileConverter.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
[XmlRoot]
[XmlType]
@@ -332,7 +328,7 @@ namespace FileConverter
{
if (string.IsNullOrEmpty(settingsKey))
{
throw new ArgumentNullException("key");
throw new ArgumentNullException(nameof(settingsKey));
}
if (value == null)
@@ -359,7 +355,7 @@ namespace FileConverter
{
if (string.IsNullOrEmpty(settingsKey))
{
throw new ArgumentNullException("key");
throw new ArgumentNullException(nameof(settingsKey));
}
if (this.settings.ContainsKey(settingsKey))
@@ -382,10 +378,10 @@ namespace FileConverter
Type type = typeof(T);
if (type.IsEnum)
{
return (T)System.Enum.Parse(type, settingsValue);
return (T)Enum.Parse(type, settingsValue);
}
return (T)System.Convert.ChangeType(settingsValue, type, NumberFormatInfo.InvariantInfo);
return (T)Convert.ChangeType(settingsValue, type, NumberFormatInfo.InvariantInfo);
}
public bool IsRelevantSetting(string settingsKey)
@@ -16,13 +16,7 @@ namespace FileConverter
this.value = value;
}
public int Count
{
get
{
return 1;
}
}
public int Count => 1;
public IEnumerable<string> Keys
{
+4 -10
View File
@@ -12,10 +12,10 @@ namespace FileConverter.Diagnostics
public static class Debug
{
private static string diagnosticsFolderPath;
private static Dictionary<int, DiagnosticsData> diagnosticsDataById = new Dictionary<int, DiagnosticsData>();
private static readonly string diagnosticsFolderPath;
private static readonly Dictionary<int, DiagnosticsData> diagnosticsDataById = new Dictionary<int, DiagnosticsData>();
private static int threadCount = 0;
private static int mainThreadId = 0;
private static readonly int mainThreadId = 0;
static Debug()
{
@@ -45,13 +45,7 @@ namespace FileConverter.Diagnostics
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static DiagnosticsData[] Data
{
get
{
return Debug.diagnosticsDataById.Values.ToArray();
}
}
public static DiagnosticsData[] Data => Debug.diagnosticsDataById.Values.ToArray();
public static void Log(string message, params object[] arguments)
{
@@ -13,8 +13,8 @@ namespace FileConverter.Diagnostics
public class DiagnosticsData : INotifyPropertyChanged
{
private List<string> logMessages = new List<string>();
private StringBuilder stringBuilder = new StringBuilder();
private readonly List<string> logMessages = new List<string>();
private readonly StringBuilder stringBuilder = new StringBuilder();
private System.IO.TextWriter logFileWriter;
private string name;
@@ -28,10 +28,7 @@ namespace FileConverter.Diagnostics
public string Name
{
get
{
return this.name;
}
get => this.name;
private set
{
@@ -62,7 +59,7 @@ namespace FileConverter.Diagnostics
public void Initialize(string diagnosticsFolderPath, int id)
{
string path = Path.Combine(diagnosticsFolderPath, string.Format("Diagnostics{0}.log", id));
string path = Path.Combine(diagnosticsFolderPath, $"Diagnostics{id}.log");
path = PathHelpers.GenerateUniquePath(path);
this.logFileWriter = new StreamWriter(File.Open(path, FileMode.Create));
@@ -81,7 +78,7 @@ namespace FileConverter.Diagnostics
this.logFileWriter.WriteLine(log);
this.logFileWriter.Flush();
this.OnPropertyChanged("Content");
this.OnPropertyChanged(nameof(this.Content));
}
[NotifyPropertyChangedInvocator]
+3 -3
View File
@@ -129,7 +129,7 @@ namespace FileConverter
}
catch (Exception)
{
Debug.Log(string.Format("Can't create directories for path {0}", filePath));
Debug.Log($"Can't create directories for path {filePath}");
return false;
}
}
@@ -213,8 +213,8 @@ namespace FileConverter
for (int index = 0; index < directories.Length; index++)
{
outputPath = outputPath.Replace(string.Format("(d{0})", directories.Length - index - 1), directories[index]);
outputPath = outputPath.Replace(string.Format("(D{0})", directories.Length - index - 1), directories[index].ToUpperInvariant());
outputPath = outputPath.Replace($"(d{directories.Length - index - 1})", directories[index]);
outputPath = outputPath.Replace($"(D{directories.Length - index - 1})", directories[index].ToUpperInvariant());
}
outputPath = outputPath.Replace("(n:i)", numberIndex.ToString());
@@ -4,9 +4,6 @@ namespace FileConverter.Services
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using FileConverter.Annotations;
@@ -2,12 +2,8 @@
namespace FileConverter.Services
{
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Xml.Serialization;
using FileConverter.Annotations;
using GalaSoft.MvvmLight;
public class UpgradeVersionDescription : ObservableObject
@@ -52,10 +48,7 @@ namespace FileConverter.Services
[XmlIgnore]
public bool InstallerDownloadInProgress
{
get
{
return this.installerDownloadInProgress;
}
get => this.installerDownloadInProgress;
set
{
@@ -69,10 +62,7 @@ namespace FileConverter.Services
[XmlIgnore]
public int InstallerDownloadProgress
{
get
{
return this.installerDownloadProgress;
}
get => this.installerDownloadProgress;
set
{
@@ -84,22 +74,10 @@ namespace FileConverter.Services
}
[XmlIgnore]
public bool InstallerDownloadDone
{
get
{
return !this.InstallerDownloadInProgress && this.InstallerDownloadProgress == 100;
}
}
public bool InstallerDownloadDone => !this.InstallerDownloadInProgress && this.InstallerDownloadProgress == 100;
[XmlIgnore]
public bool InstallerDownloadNotStarted
{
get
{
return !this.InstallerDownloadInProgress && this.InstallerDownloadProgress == 0;
}
}
public bool InstallerDownloadNotStarted => !this.InstallerDownloadInProgress && this.InstallerDownloadProgress == 0;
[XmlIgnore]
public bool NeedToUpgrade
@@ -17,7 +17,7 @@ namespace FileConverter.ValueConverters
FileConverter.Version version = (FileConverter.Version)value;
return string.Format("File Converter v{0}", version.ToString());
return $"File Converter v{version}";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@@ -17,7 +17,7 @@ namespace FileConverter.ValueConverters
double bitrate = (double)value;
return string.Format("{0} kbit/s", bitrate.ToString("0"));
return $"{bitrate:0} kbit/s";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@@ -17,7 +17,7 @@ namespace FileConverter.ValueConverters.Generic
double doubleValue = (double)value;
return string.Format("{0} second{1}", doubleValue.ToString("0.#"), doubleValue >= 2 ? "s" : string.Empty);
return $"{doubleValue:0.#} second{(doubleValue >= 2 ? "s" : string.Empty)}";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@@ -12,7 +12,7 @@ namespace FileConverter.ValueConverters.Generic
{
if (value == null)
{
throw new System.ArgumentNullException("value.");
throw new System.ArgumentNullException(nameof(value));
}
string stringValue = value.ToString();
@@ -20,7 +20,7 @@ namespace FileConverter.ValueConverters.Generic
string typeName = parameter as string;
if (typeName == null)
{
throw new ArgumentNullException("parameter", "The parameter must contains the enum type.");
throw new ArgumentNullException(nameof(parameter), "The parameter must contains the enum type.");
}
Type enumType = Type.GetType(typeName);
@@ -18,7 +18,7 @@ namespace FileConverter.ValueConverters.Generic
string typeName = parameter as string;
if (typeName == null)
{
throw new ArgumentNullException("parameter", "The parameter must contains a convertible type.");
throw new ArgumentNullException(nameof(parameter), "The parameter must contains a convertible type.");
}
Type type = Type.GetType(typeName);
@@ -23,8 +23,7 @@ namespace FileConverter.ValueConverters
return "Hidden";
}
OutputType referenceType;
if (!Enum.TryParse<OutputType>(referenceTypeName, out referenceType))
if (!Enum.TryParse(referenceTypeName, out OutputType referenceType))
{
return "Hidden";
}
+2 -2
View File
@@ -49,10 +49,10 @@ namespace FileConverter
{
if (this.Patch == 0)
{
return string.Format("{0}.{1}", this.Major, this.Minor);
return $"{this.Major}.{this.Minor}";
}
return string.Format("{0}.{1}.{2}", this.Major, this.Minor, this.Patch);
return $"{this.Major}.{this.Minor}.{this.Patch}";
}
public override bool Equals(object obj)
@@ -2,8 +2,6 @@
namespace FileConverter.ViewModels
{
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;
using CommonServiceLocator;
@@ -1,14 +1,11 @@
// <copyright file="InputExtensionCategory.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
using System.ComponentModel;
using CommonServiceLocator;
using FileConverter.Services;
using GalaSoft.MvvmLight.Ioc;
namespace FileConverter.ViewModels
{
using System.Collections.Generic;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using System.ComponentModel;
using System.Collections.Generic;
public class InputExtensionCategory : ObservableObject
{
@@ -58,10 +58,7 @@ namespace FileConverter.ViewModels
public string InformationMessage
{
get
{
return this.informationMessage;
}
get => this.informationMessage;
private set
{
@@ -71,10 +68,7 @@ namespace FileConverter.ViewModels
public ObservableCollection<ConversionJob> ConversionJobs
{
get
{
return this.conversionJobs;
}
get => this.conversionJobs;
private set
{
@@ -1,6 +1,5 @@
// <copyright file="PresetNode.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
@@ -143,10 +142,7 @@ namespace FileConverter.ViewModels
public string OutputFileNameTemplate
{
get
{
return this.Preset.OutputFileNameTemplate;
}
get => this.Preset.OutputFileNameTemplate;
set
{
@@ -3,7 +3,6 @@
namespace FileConverter.ViewModels
{
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Input;
using FileConverter.Services;
@@ -9,7 +9,7 @@
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d" x:Class="FileConverter.Views.MainWindow"
Height="500" Width="950" MinHeight="250" MinWidth="500" WindowStartupLocation="CenterScreen" Icon="/FileConverter;component/Resources/ApplicationIcon.ico"
Height="500" Width="950" MinHeight="480" MinWidth="640" WindowStartupLocation="CenterScreen" Icon="/FileConverter;component/Resources/ApplicationIcon.ico"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
@@ -46,10 +46,10 @@
<Label Content="{x:Static project:Resources.ConversionQueueTitle}" VerticalAlignment="Center" FontSize="18" FontWeight="Bold"/>
<Label Content="{Binding InformationMessage}" VerticalAlignment="Center" FontSize="13.333" HorizontalAlignment="Right"/>
</DockPanel>
<Button Grid.Column="1" Margin="3,3" Command="{Binding ShowDiagnosticsCommand}" ToolTip="{x:Static project:Resources.DiagnosticsButtonTooltip}">
<Button Grid.Column="1" Margin="3" Command="{Binding ShowDiagnosticsCommand}" ToolTip="{x:Static project:Resources.DiagnosticsButtonTooltip}">
<Image HorizontalAlignment="Left" Height="48" Width="48" VerticalAlignment="Top" Source="/Resources/LogIcon.png" />
</Button>
<Button Grid.Column="2" Margin="3,3" Command="{Binding ShowSettingsCommand}" ToolTip="{x:Static project:Resources.SettingsButtonTooltip}">
<Button Grid.Column="2" Margin="3" Command="{Binding ShowSettingsCommand}" ToolTip="{x:Static project:Resources.SettingsButtonTooltip}">
<Image HorizontalAlignment="Left" Height="48" Width="48" VerticalAlignment="Top" Source="/Resources/SettingsIcon.ico" />
</Button>
</Grid>
+3 -3
View File
@@ -14,7 +14,7 @@ namespace FileConverter
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
throw new ArgumentNullException(nameof(path));
}
XmlRootAttribute xmlRoot = new XmlRootAttribute
@@ -55,12 +55,12 @@ namespace FileConverter
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
throw new ArgumentNullException(nameof(path));
}
if (objectToSerialize == null)
{
throw new ArgumentNullException("objectToSerialize");
throw new ArgumentNullException(nameof(objectToSerialize));
}
XmlRootAttribute xmlRoot = new XmlRootAttribute
@@ -282,21 +282,22 @@ namespace FileConverterExtension
{
if (string.IsNullOrEmpty(this.FileConverterPath))
{
MessageBox.Show(string.Format("Can't retrieve the file converter executable path. You should try to reinstall the application."));
MessageBox.Show("Can't retrieve the file converter executable path. You should try to reinstall the application.");
return;
}
if (!File.Exists(this.FileConverterPath))
{
MessageBox.Show(string.Format("Can't find the file converter executable ({0}). You should try to reinstall the application.", this.FileConverterPath));
MessageBox.Show($"Can't find the file converter executable ({this.FileConverterPath}). You should try to reinstall the application.");
return;
}
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath);
processStartInfo.CreateNoWindow = false;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = false;
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath)
{
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardOutput = false,
};
// Build arguments string.
StringBuilder stringBuilder = new StringBuilder();
@@ -310,21 +311,22 @@ namespace FileConverterExtension
{
if (string.IsNullOrEmpty(this.FileConverterPath))
{
MessageBox.Show(string.Format("Can't retrieve the file converter executable path. You should try to reinstall the application."));
MessageBox.Show("Can't retrieve the file converter executable path. You should try to reinstall the application.");
return;
}
if (!File.Exists(this.FileConverterPath))
{
MessageBox.Show(string.Format("Can't find the file converter executable ({0}). You should try to reinstall the application.", this.FileConverterPath));
MessageBox.Show($"Can't find the file converter executable ({this.FileConverterPath}). You should try to reinstall the application.");
return;
}
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath);
processStartInfo.CreateNoWindow = false;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = false;
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath)
{
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardOutput = false,
};
// Build arguments string.
StringBuilder stringBuilder = new StringBuilder();
@@ -14,19 +14,16 @@ namespace FileConverterExtension
public string FullName
{
get;
private set;
}
public string Name
{
get;
private set;
}
public string[] Folders
{
get;
private set;
}
public bool Enabled