diff --git a/LibMpc/Commands/Commands.Database.cs b/LibMpc/Commands/Commands.Database.cs index 1db9e4d..fe9eb31 100644 --- a/LibMpc/Commands/Commands.Database.cs +++ b/LibMpc/Commands/Commands.Database.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using LibMpc.Types; namespace LibMpc @@ -29,21 +30,21 @@ namespace LibMpc { var results = new List(); - var fileBuilder = new MpdFileBuidler(); + var mpdFile = MpdFile.EmptyFile; foreach (var line in response) { if (line.Key.Equals("file")) { - if (fileBuilder.IsInitialized) + if (mpdFile.IsInitialized) { - results.Add(fileBuilder.Build()); + results.Add(mpdFile); } - fileBuilder.Init(line.Value); + mpdFile = new MpdFile(line.Value); } else { - fileBuilder.WithProperty(line.Key, line.Value); + mpdFile.AddTag(line.Key, line.Value); } } @@ -71,25 +72,27 @@ namespace LibMpc // TODO: findadd - public class ListAll : IMpcCommand + public class ListAll : IMpcCommand> { public string Value => "listall"; - public MpdDirectory FormatResponse(IList> response) + public IEnumerable FormatResponse(IList> response) { - // Add by default the root directory - var rootDirectory = new MpdDirectory("/"); + var rootDirectory = new List + { + new MpdDirectory("/") // Add by default the root directory + }; foreach (var line in response) { if (line.Key.Equals("file")) { - rootDirectory.AddFile(line.Value); + rootDirectory.Last().AddFile(line.Value); } if (line.Key.Equals("directory")) { - rootDirectory.AddDirectory(line.Value); + rootDirectory.Add(new MpdDirectory(line.Value)); } } diff --git a/LibMpc/Types/MpdDirectory.cs b/LibMpc/Types/MpdDirectory.cs index f71e2a9..561288f 100644 --- a/LibMpc/Types/MpdDirectory.cs +++ b/LibMpc/Types/MpdDirectory.cs @@ -6,7 +6,6 @@ namespace LibMpc.Types public class MpdDirectory { private readonly IList _files = new List(); - private readonly IList _subDirectories = new List(); public MpdDirectory(string path) { @@ -21,50 +20,10 @@ namespace LibMpc.Types public string Path { get; } public string Name { get; } public IEnumerable Files => _files; - public IEnumerable SubDirectories => _subDirectories; internal void AddFile(string file) { - var filePath = file.Split('/'); - var name = filePath[filePath.Length - 1]; - - if (filePath.Length == 1) - { - _files.Add(new MpdFile(name)); - } - else - { - var filePathWithoutCurrentDirectory = string.Join("/", filePath.Skip(1)); - foreach (var subDirectory in _subDirectories) - { - if (subDirectory.Path.Equals(filePath[0])) - { - subDirectory.AddFile(filePathWithoutCurrentDirectory); - } - } - } - } - - internal void AddDirectory(string directory) - { - var directoryPath = directory.Split('/'); - var name = directoryPath[directoryPath.Length - 1]; - - if (directoryPath.Length == 1) - { - _subDirectories.Add(new MpdDirectory(name)); - } - else - { - var directoryPathWithoutCurrentDirectory = string.Join("/", directoryPath.Skip(1)); - foreach (var subDirectory in _subDirectories) - { - if (subDirectory.Path.Equals(directoryPath[0])) - { - subDirectory.AddDirectory(directoryPathWithoutCurrentDirectory); - } - } - } + _files.Add(new MpdFile(file)); } } } \ No newline at end of file diff --git a/LibMpc/Types/MpdFile.cs b/LibMpc/Types/MpdFile.cs index 204b828..12a15c3 100644 --- a/LibMpc/Types/MpdFile.cs +++ b/LibMpc/Types/MpdFile.cs @@ -1,8 +1,14 @@ -namespace LibMpc.Types +using System.Collections.Generic; + +namespace LibMpc.Types { - public class MpdFileBuidler + /// + /// The MpdFile class contains all meta data for a file of the MPD. + /// + public class MpdFile { - private const string TagFile = "file"; + public static readonly MpdFile EmptyFile = new MpdFile(string.Empty); + private const string TagTime = "Time"; private const string TagArtist = "Artist"; private const string TagAlbum = "Album"; @@ -18,56 +24,83 @@ private const string TagPos = "Pos"; private const string TagId = "Id"; - private MpdFile _mpdFile; + private readonly IDictionary _unknownTags = new Dictionary(); - public bool IsInitialized => _mpdFile != null; - - public MpdFileBuidler Init(string file) - { - _mpdFile = new MpdFile(file); - return this; - } - - public MpdFileBuidler WithProperty(string tag, string value) - { - _mpdFile.CheckNotNull(); - - // TODO: Parse tag - - return this; - } - - public MpdFile Build() - { - return _mpdFile; - } - } - - - /// - /// The MpdFile class contains all meta data for a file of the MPD. - /// - public class MpdFile - { public MpdFile(string file) { File = file; + IsInitialized = !string.IsNullOrEmpty(File); } public string File { get; } - public int Time { get; internal set; } = -1; - public string Album { get; internal set; } = string.Empty; - public string Artist { get; internal set; } = string.Empty; - public string Title { get; internal set; } = string.Empty; - public string Track { get; internal set; } = string.Empty; - public string Name { get; internal set; } = string.Empty; - public string Genre { get; internal set; } = string.Empty; - public string Date { get; internal set; } = string.Empty; - public string Composer { get; internal set; } = string.Empty; - public string Performer { get; internal set; } = string.Empty; - public string Comment { get; internal set; } = string.Empty; - public int Disc { get; internal set; } = -1; - public int Pos { get; internal set; } = -1; - public int Id { get; internal set; } = -1; + public int Time { get; private set; } = -1; + public string Album { get; private set; } = string.Empty; + public string Artist { get; private set; } = string.Empty; + public string Title { get; private set; } = string.Empty; + public string Track { get; private set; } = string.Empty; + public string Name { get; private set; } = string.Empty; + public string Genre { get; private set; } = string.Empty; + public string Date { get; private set; } = string.Empty; + public string Composer { get; private set; } = string.Empty; + public string Performer { get; private set; } = string.Empty; + public string Comment { get; private set; } = string.Empty; + public int Disc { get; private set; } = -1; + public int Pos { get; private set; } = -1; + public int Id { get; private set; } = -1; + public IDictionary UnknownTags => _unknownTags; + + internal bool IsInitialized { get; } + + internal void AddTag(string tag, string value) + { + switch (tag) + { + case TagTime: + Time = int.Parse(value); + break; + case TagArtist: + Artist = value; + break; + case TagAlbum: + Album = value; + break; + case TagTitle: + Title = value; + break; + case TagTrack: + Track = value; + break; + case TagName: + Name = value; + break; + case TagGenre: + Genre = value; + break; + case TagDate: + Date = value; + break; + case TagComposer: + Composer = value; + break; + case TagPerformer: + Performer = value; + break; + case TagComment: + Comment = value; + break; + case TagDisc: + Disc = int.Parse(value); + break; + case TagPos: + Pos = int.Parse(value); + break; + case TagId: + Id = int.Parse(value); + break; + default: + _unknownTags.Add(tag, value); + break; + } + } } } diff --git a/LibMpcTest/LibMpcTest.cs b/LibMpcTest/LibMpcTest.cs index ee4408e..4a2332d 100644 --- a/LibMpcTest/LibMpcTest.cs +++ b/LibMpcTest/LibMpcTest.cs @@ -53,8 +53,18 @@ namespace LibMpcTest WriteLine("ListAllTest Result:"); WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); - Assert.True(response.Response.Body.SubDirectories.Count().Equals(4)); - Assert.True(response.Response.Body.Files.Count().Equals(3)); + Assert.True(response.Response.Body.Count().Equals(7)); + } + + [Fact] + public async Task FindGenreTest() + { + var response = await _mpc.SendAsync(new Commands.Database.Find(MpdTags.Genre, "soundfx")); + + WriteLine("FindGenreTest Result:"); + WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + Assert.True(response.Response.Body.Count().Equals(6)); } public void Dispose() @@ -64,11 +74,7 @@ namespace LibMpcTest private void WriteLine(string value) { -#if DEBUG - Debug.WriteLine(value); -#else Console.Out.WriteLine(value); -#endif } } }