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

Edit file content during textimport #371

Merged
merged 9 commits into from
Jun 19, 2024
Merged
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
8 changes: 8 additions & 0 deletions AudioCuesheetEditor/AudioCuesheetEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<Content Remove="Resources\Localization\EditSections\en.json" />
<Content Remove="Resources\Localization\EditTrackModal\de.json" />
<Content Remove="Resources\Localization\EditTrackModal\en.json" />
<Content Remove="Resources\Localization\ImportFileView\de.json" />
<Content Remove="Resources\Localization\ImportFileView\en.json" />
<Content Remove="Resources\Localization\MainLayout\de.json" />
<Content Remove="Resources\Localization\MainLayout\en.json" />
<Content Remove="Resources\Localization\ModalDialog\de.json" />
Expand Down Expand Up @@ -66,6 +68,8 @@
<EmbeddedResource Include="Resources\Localization\EditSections\en.json" />
<EmbeddedResource Include="Resources\Localization\EditTrackModal\de.json" />
<EmbeddedResource Include="Resources\Localization\EditTrackModal\en.json" />
<EmbeddedResource Include="Resources\Localization\ImportFileView\de.json" />
<EmbeddedResource Include="Resources\Localization\ImportFileView\en.json" />
<EmbeddedResource Include="Resources\Localization\MainLayout\en.json" />
<EmbeddedResource Include="Resources\Localization\MainLayout\de.json" />
<EmbeddedResource Include="Resources\Localization\ModalDialog\en.json" />
Expand Down Expand Up @@ -116,5 +120,9 @@
<ItemGroup>
<ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" />
</ItemGroup>

<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions AudioCuesheetEditor/Extensions/SessionStateContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ public CuesheetImportfile? CuesheetImportFile
get { return cuesheetImportFile; }
set
{
if (cuesheetImportFile != null)
{
cuesheetImportFile.AnalysisFinished -= CuesheetImportFile_AnalysisFinished;
}
cuesheetImportFile = value;
if ((CuesheetImportFile != null) && (CuesheetImportFile.Cuesheet != null))
{
CuesheetImportFile.AnalysisFinished += CuesheetImportFile_AnalysisFinished;
ImportCuesheet = CuesheetImportFile.Cuesheet;
}
else
Expand Down Expand Up @@ -128,6 +133,22 @@ public ViewMode CurrentViewMode
}
}

public IImportfile? Importfile
{
get
{
if (TextImportFile != null)
{
return TextImportFile;
}
if (CuesheetImportFile != null)
{
return CuesheetImportFile;
}
return null;
}
}

public void ResetImport()
{
TextImportFile = null;
Expand Down Expand Up @@ -169,5 +190,17 @@ private void Cuesheet_CuesheetImported(object? sender, EventArgs e)
{
CuesheetChanged?.Invoke(this, EventArgs.Empty);
}

void CuesheetImportFile_AnalysisFinished(object? sender, EventArgs e)
{
if (CuesheetImportFile != null)
{
ImportCuesheet = CuesheetImportFile.Cuesheet;
}
else
{
ImportCuesheet = null;
}
}
}
}
80 changes: 51 additions & 29 deletions AudioCuesheetEditor/Model/IO/Import/CuesheetImportfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,49 @@

namespace AudioCuesheetEditor.Model.IO.Import
{
public class CuesheetImportfile
public class CuesheetImportfile : IImportfile
{
/// <summary>
/// File content (each element is a file line)
/// </summary>
public IReadOnlyCollection<String?>? FileContent { get; private set; }
private IEnumerable<String?> fileContent;

/// <summary>
/// File content with marking which passages has been reconized by scheme
/// </summary>
public IReadOnlyCollection<String?>? FileContentRecognized { get; private set; }
public EventHandler? AnalysisFinished;

/// <inheritdoc />
public IEnumerable<String?> FileContent
{
get => fileContent;
set
{
fileContent = value;
Analyse();
}
}

/// <inheritdoc />
public IEnumerable<String?> FileContentRecognized { get; private set; }
public Exception? AnalyseException { get; private set; }
public Cuesheet? Cuesheet { get; private set; }
public ApplicationOptions ApplicationOptions { get; private set; }

public CuesheetImportfile(MemoryStream fileContentStream, ApplicationOptions applicationOptions)
{
FileContentRecognized = [];
fileContentStream.Position = 0;
using var reader = new StreamReader(fileContentStream);
List<String?> lines = [];
while (reader.EndOfStream == false)
{
lines.Add(reader.ReadLine());
}
fileContent = lines.AsReadOnly();
ApplicationOptions = applicationOptions;
Analyse();
}

public CuesheetImportfile(MemoryStream fileContent, ApplicationOptions applicationOptions)
private void Analyse()
{
try
{
Cuesheet = new Cuesheet();
fileContent.Position = 0;
using var reader = new StreamReader(fileContent);
var cuesheetArtistGroupName = "CuesheetArtist";
var cuesheetTitleGroupName = "CuesheetTitle";
var cuesheetFileNameGroupName = "CuesheetFileName";
Expand All @@ -65,11 +87,10 @@ public CuesheetImportfile(MemoryStream fileContent, ApplicationOptions applicati
var regexCDTextfile = new Regex("^" + CuesheetConstants.CuesheetCDTextfile + " \"(?'" + cuesheetCDTextfileGroupName + "'.{0,})\"");
var regexCatalogueNumber = new Regex("^" + CuesheetConstants.CuesheetCatalogueNumber + " (?'" + cuesheetCatalogueNumberGroupName + "'.{0,})");
Track? track = null;
List<String?> lines = new();
List<String?>? recognizedLines = new();
while (reader.EndOfStream == false)
List<String?> lines = [];
List<String?>? recognizedLines = [];
foreach (var line in FileContent)
{
var line = reader.ReadLine();
lines.Add(line);
String? recognizedLine = line;
if (String.IsNullOrEmpty(line) == false)
Expand Down Expand Up @@ -214,9 +235,9 @@ public CuesheetImportfile(MemoryStream fileContent, ApplicationOptions applicati
var matchGroup = match.Groups.GetValueOrDefault(trackPreGapGroupName);
if (matchGroup != null)
{
var minutes = int.Parse(matchGroup.Value.Substring(0, matchGroup.Value.IndexOf(":")));
var seconds = int.Parse(matchGroup.Value.Substring(matchGroup.Value.IndexOf(":") + 1, 2));
var frames = int.Parse(matchGroup.Value.Substring(matchGroup.Value.LastIndexOf(":") + 1));
var minutes = int.Parse(matchGroup.Value.Substring(0, matchGroup.Value.IndexOf(':')));
var seconds = int.Parse(matchGroup.Value.Substring(matchGroup.Value.IndexOf(':') + 1, 2));
var frames = int.Parse(matchGroup.Value.Substring(matchGroup.Value.LastIndexOf(':') + 1));
if (track != null)
{
track.PreGap = new TimeSpan(0, 0, minutes, seconds, Convert.ToInt32((frames / 75.0) * 1000));
Expand All @@ -238,9 +259,9 @@ public CuesheetImportfile(MemoryStream fileContent, ApplicationOptions applicati
var matchGroup = match.Groups.GetValueOrDefault(trackIndex01GroupName);
if (matchGroup != null)
{
var minutes = int.Parse(matchGroup.Value.Substring(0, matchGroup.Value.IndexOf(":")));
var seconds = int.Parse(matchGroup.Value.Substring(matchGroup.Value.IndexOf(":") + 1, 2));
var frames = int.Parse(matchGroup.Value.Substring(matchGroup.Value.LastIndexOf(":") + 1));
var minutes = int.Parse(matchGroup.Value.Substring(0, matchGroup.Value.IndexOf(':')));
var seconds = int.Parse(matchGroup.Value.Substring(matchGroup.Value.IndexOf(':') + 1, 2));
var frames = int.Parse(matchGroup.Value.Substring(matchGroup.Value.LastIndexOf(':') + 1));
if (track != null)
{
track.Begin = new TimeSpan(0, 0, minutes, seconds, Convert.ToInt32((frames / 75.0) * 1000));
Expand All @@ -256,7 +277,7 @@ public CuesheetImportfile(MemoryStream fileContent, ApplicationOptions applicati
}
if (track != null)
{
Cuesheet.AddTrack(track, applicationOptions);
Cuesheet.AddTrack(track, ApplicationOptions);
}
else
{
Expand All @@ -270,9 +291,9 @@ public CuesheetImportfile(MemoryStream fileContent, ApplicationOptions applicati
var matchGroup = match.Groups.GetValueOrDefault(trackPostGapGroupName);
if (matchGroup != null)
{
var minutes = int.Parse(matchGroup.Value.Substring(0, matchGroup.Value.IndexOf(":")));
var seconds = int.Parse(matchGroup.Value.Substring(matchGroup.Value.IndexOf(":") + 1, 2));
var frames = int.Parse(matchGroup.Value.Substring(matchGroup.Value.LastIndexOf(":") + 1));
var minutes = int.Parse(matchGroup.Value.Substring(0, matchGroup.Value.IndexOf(':')));
var seconds = int.Parse(matchGroup.Value.Substring(matchGroup.Value.IndexOf(':') + 1, 2));
var frames = int.Parse(matchGroup.Value.Substring(matchGroup.Value.LastIndexOf(':') + 1));
if (track != null)
{
track.PostGap = new TimeSpan(0, 0, minutes, seconds, Convert.ToInt32((frames / 75.0) * 1000));
Expand All @@ -290,15 +311,16 @@ public CuesheetImportfile(MemoryStream fileContent, ApplicationOptions applicati
}
recognizedLines.Add(recognizedLine);
}
FileContent = lines.AsReadOnly();
fileContent = lines.AsReadOnly();
FileContentRecognized = recognizedLines.AsReadOnly();
}
catch(Exception ex)
catch (Exception ex)
{
AnalyseException = ex;
Cuesheet = null;
FileContentRecognized = null;
FileContentRecognized = FileContent;
}
AnalysisFinished?.Invoke(this, EventArgs.Empty);
}
}
}
29 changes: 29 additions & 0 deletions AudioCuesheetEditor/Model/IO/Import/IImportfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//This file is part of AudioCuesheetEditor.

//AudioCuesheetEditor is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//AudioCuesheetEditor is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
namespace AudioCuesheetEditor.Model.IO.Import
{
public interface IImportfile
{
/// <summary>
/// File content (each element is a file line)
/// </summary>
public IEnumerable<String?> FileContent { get; set; }
/// <summary>
/// File content with marking which passages has been reconized by scheme
/// </summary>
public IEnumerable<String?> FileContentRecognized { get; }
}
}
39 changes: 20 additions & 19 deletions AudioCuesheetEditor/Model/IO/Import/TextImportfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@
using AudioCuesheetEditor.Model.IO.Audio;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Model.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AudioCuesheetEditor.Model.IO.Import
{
public class TextImportfile : IDisposable
public class TextImportfile : IImportfile, IDisposable
{
public const String MimeType = "text/plain";
public const String FileExtension = ".txt";
Expand All @@ -38,12 +32,15 @@ public class TextImportfile : IDisposable
private TextImportScheme textImportScheme;
private TimeSpanFormat? timeSpanFormat;
private bool disposedValue;
private IEnumerable<String?> fileContent;

public TextImportfile(MemoryStream fileContent, ImportOptions? importOptions = null)
public TextImportfile(MemoryStream fileContentStream, ImportOptions? importOptions = null)
{
FileContentRecognized = [];
textImportScheme = new TextImportScheme();
fileContent.Position = 0;
using var reader = new StreamReader(fileContent);
fileContent = [];
fileContentStream.Position = 0;
using var reader = new StreamReader(fileContentStream);
List<String?> lines = [];
while (reader.EndOfStream == false)
{
Expand All @@ -65,15 +62,19 @@ public TextImportfile(MemoryStream fileContent, ImportOptions? importOptions = n
}
}

/// <summary>
/// File content (each element is a file line)
/// </summary>
public IReadOnlyCollection<String?> FileContent { get; private set; }
/// <inheritdoc />
public IEnumerable<String?> FileContent
{
get => fileContent;
set
{
fileContent = value;
Analyse();
}
}

/// <summary>
/// File content with marking which passages has been reconized by scheme
/// </summary>
public IReadOnlyCollection<String?>? FileContentRecognized { get; private set; }
/// <inheritdoc />
public IEnumerable<String?> FileContentRecognized { get; private set; }

public TextImportScheme TextImportScheme
{
Expand Down Expand Up @@ -129,7 +130,7 @@ private void Analyse()
try
{
Cuesheet = new Cuesheet();
FileContentRecognized = null;
FileContentRecognized = [];
AnalyseException = null;
Boolean cuesheetRecognized = false;
List<String?> recognizedFileContent = [];
Expand Down
Loading