コード例 #1
0
        /// <summary>
        /// Reads the items and their metadata from a MPD response
        /// </summary>
        /// <returns>List of MPD items with metadata</returns>
        public IEnumerable <IItem> GetListedTracks()
        {
            var result  = new List <IItem>();
            var builder = new TrackBuilder();

            foreach (var kv in _valueList)
            {
                if (kv.Key.Equals(ResponseParserKeys.File))
                {
                    if (!builder.IsEmpty())
                    {
                        result.Add(builder.Get());
                    }

                    builder.New(kv.Value);
                }
                else
                {
                    if (!builder.IsEmpty())
                    {
                        builder.Add(kv);
                    }
                }
            }

            if (!builder.IsEmpty())
            {
                result.Add(builder.Get());
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Parses a directory list MPD response into directory and item DTOs
        /// TODO: add playlist recognition
        /// TODO: recreate dir structure
        /// </summary>
        /// <param name="path">The requested path from the command</param>
        /// <returns>IDirectory instance</returns>
        public IDirectory GetDirectoryListing(string path = "")
        {
            var split  = splitPath(path);
            var result = new Directory {
                Name = split.Last(), Path = path
            };
            var builder = new TrackBuilder();

            // we will skip the first line of the response containing the entry for the requested dir
            var skip = 1;

            // check if there is a second line wit a datetime for the requested dir
            if (_valueList.Count >= 2 && _valueList.ElementAt(1).Key.ToLower().StartsWith(ResponseParserKeys.LastModified))
            {
                result.LastModified = GetLastModified(_valueList.ElementAt(1).Value);
                // if yes, skip this line as well
                skip = 2;
            }

            // the current dir being looked at
            IDirectory currentDirectory = null;

            // keeps track if the current section is a directory or not
            bool currentSectionIsDirectory = true;

            foreach (var kv in _valueList.Skip(skip))
            {
                var key = kv.Key.ToLower();

                // if we have been collecting metadata for a file and now there is a new file/directory section,
                // add the current file to the currently parsed direcory
                if (key == ResponseParserKeys.Directory || key == ResponseParserKeys.File)
                {
                    if (!builder.IsEmpty())
                    {
                        if (currentDirectory == null)
                        {
                            result.Files.Add(builder.Get());
                        }
                        else
                        {
                            currentDirectory.Files.Add(builder.Get());
                        }

                        builder.Clear();
                    }

                    // only do it if the current dir is _not_ the main entry dir
                    if (currentDirectory != null)
                    {
                        result.Directories.Add(currentDirectory);
                    }
                }

                // a new directory section begins
                // start a new directory dto, add the last one to the main directory
                if (key == ResponseParserKeys.Directory)
                {
                    currentSectionIsDirectory = true;

                    var newDirValues = splitPath(kv.Value);
                    currentDirectory = new Directory {
                        Name = newDirValues.Last(), Path = kv.Value
                    };
                }
                // a new file section begins
                // start a new file dto
                else if (key == ResponseParserKeys.File)
                {
                    currentSectionIsDirectory = false;

                    // having added the last file to the current dir at the top,
                    // we start reading a new one here
                    builder.New(kv.Value);
                }
                // neither file nor dir, this is a metadata value for something.
                // add data to the current file, unless we are in a directory section
                else
                {
                    if (currentSectionIsDirectory)
                    {
                        // in directory sections, this can only be a last-modified
                        if (key == ResponseParserKeys.LastModified)
                        {
                            currentDirectory.LastModified = GetLastModified(kv.Value);
                        }
                    }
                    else
                    {
                        builder.Add(kv);
                    }
                }
            }

            // add the last element in the pipeline
            if (!currentSectionIsDirectory && !builder.IsEmpty())
            {
                currentDirectory.Files.Add(builder.Get());
            }

            if (currentSectionIsDirectory && currentDirectory != null)
            {
                result.Directories.Add(currentDirectory);
            }

            return(result);
        }