示例#1
0
        /// <summary>
        /// Retrieves a list of all tracks found in the given playlist.
        /// </summary>
        /// <param name="playlistID">The unique PersistentID of the playlist to examine.</param>
        /// <returns></returns>
        /// <remarks>
        /// iTunes allows users to create multiple playlists with the same name.  So we
        /// must use the PersistentID of the playlist instead of its canonical name.
        /// </remarks>

        public override PersistentIDCollection FindTracksByPlaylist(PersistentID playlistID)
        {
            if (!isReady)
            {
                return(new PersistentIDCollection());
            }

            // find the <plist><dict><key>Playlists</key><array> root node
            var playlistRoot =
                from node in root
                .Element(ns + "dict")
                .Elements(ns + "key")
                where node.Value == "Playlists"
                select node.NextNode;

            // find the parent <array><dict> node of the named playlist
            // <array><dict><key>Name</key><string>Library</string>
            var playlistNodes =
                from node in ((XElement)playlistRoot.Single())
                .Elements(ns + "dict")
                .Elements(ns + "key")
                where node.Value == "Playlist Persistent ID" &&
                ((XElement)node.NextNode).Value == (string)playlistID
                select node.Parent;

            // collect all Track ID values from this playlist
            var trackIDs =
                from node in ((XElement)playlistNodes.Single())
                .Elements(ns + "array")
                .Elements(ns + "dict")
                .Elements(ns + "key")
                where node.Value == "Track ID"
                select node.NextNode;

            // find the <plist><dict><key>Tracks</key><dict> root node
            var trackRoot =
                from node in root
                .Element(ns + "dict")
                .Elements(ns + "key")
                where node.Value == "Tracks"
                select node.NextNode;

            // join tracks on trackID to extract the persistent IDs
            var tracks =
                from node in
                (from node in ((XElement)trackRoot.Single()).Elements(ns + "key")
                 join id in trackIDs on((XElement)node).Value equals((XElement)id).Value
                 select((XElement)node.NextNode)
                ).Elements(ns + "key")
                where ((XElement)node).Value == "Persistent ID"
                select PersistentID.Parse(((XElement)node.NextNode).Value);

            PersistentIDCollection list = new PersistentIDCollection(tracks.ToList <PersistentID>());

            return(list);
        }
示例#2
0
        /// <summary>
        /// Retrieves a list of all tracks by the specivied artist on the named album.
        /// </summary>
        /// <param name="album">The name of the album.</param>
        /// <param name="artist">The name of the artist.</param>
        /// <returns></returns>

        public override PersistentIDCollection FindTracksByAlbum(string album, string artist)
        {
            if (!isReady)
            {
                return(new PersistentIDCollection());
            }

            album  = album.Trim().ToLower();
            artist = artist.Trim().ToLower();

            // find the <plist><dict><key>Tracks</key><dict> root node
            var trackRoot =
                from node in root
                .Element(ns + "dict")
                .Elements(ns + "key")
                where node.Value == "Tracks"
                select node.NextNode;

            // Tracks/dict is the container for all tracks where each is a key/dict pair
            // collect all dict elements related to specified album

            var albumTracks =
                from node in
                (from node in ((XElement)trackRoot.Single())
                 .Elements(ns + "dict")
                 .Elements(ns + "key")
                 where node.Value == "Album" &&
                 ((XElement)node.NextNode).Value.Trim().ToLower() == album
                 select node.Parent
                ).Elements(ns + "key")
                where node.Value == "Artist" &&
                ((XElement)node.NextNode).Value.Trim().ToLower() == artist
                select node.Parent;

            // collect all persistent IDs from these dict elements
            var tracks =
                from node in albumTracks.Elements(ns + "key")
                where node.Value == "Persistent ID"
                select PersistentID.Parse(((XElement)node.NextNode).Value);

            PersistentIDCollection list = new PersistentIDCollection(tracks.ToList <PersistentID>());

            return(list);
        }