Make factory samples relative (#3510)

* Make factory samples relative
Fixes #3491
Related #1719
This commit is contained in:
Tres Finocchiaro
2017-05-06 04:27:18 -04:00
committed by Lukas W
parent 6a5255e379
commit 7e3ee14cf1
4 changed files with 76 additions and 12 deletions

View File

@@ -63,7 +63,12 @@ ConfigManager::ConfigManager() :
// If we're in development (lmms is not installed) let's get the source and
// binary directories by reading the CMake Cache
QFile cmakeCache(qApp->applicationDirPath() + "/CMakeCache.txt");
QDir appPath = qApp->applicationDirPath();
// If in tests, get parent directory
if (appPath.dirName() == "tests") {
appPath.cdUp();
}
QFile cmakeCache(appPath.absoluteFilePath("CMakeCache.txt"));
if (cmakeCache.exists()) {
cmakeCache.open(QFile::ReadOnly);
QTextStream stream(&cmakeCache);

View File

@@ -1411,25 +1411,35 @@ void SampleBuffer::setReversed( bool _on )
QString SampleBuffer::tryToMakeRelative( const QString & _file )
QString SampleBuffer::tryToMakeRelative( const QString & file )
{
if( QFileInfo( _file ).isRelative() == false )
if( QFileInfo( file ).isRelative() == false )
{
QString f = QString( _file ).replace( QDir::separator(), '/' );
QString fsd = ConfigManager::inst()->factorySamplesDir();
QString usd = ConfigManager::inst()->userSamplesDir();
fsd.replace( QDir::separator(), '/' );
usd.replace( QDir::separator(), '/' );
if( f.startsWith( fsd ) )
QString f = QString( file ).replace( QDir::separator(), '/' );
// First, look in factory samples
// Isolate "samples/" from "data:/samples/"
QString samplesSuffix = ConfigManager::inst()->factorySamplesDir().mid( ConfigManager::inst()->dataDir().length() );
// Iterate over all valid "data:/" searchPaths
for ( const QString & path : QDir::searchPaths( "data" ) )
{
return QString( f ).mid( fsd.length() );
QString samplesPath = QString( path + samplesSuffix ).replace( QDir::separator(), '/' );
if ( f.startsWith( samplesPath ) )
{
return QString( f ).mid( samplesPath.length() );
}
}
else if( f.startsWith( usd ) )
// Next, look in user samples
QString usd = ConfigManager::inst()->userSamplesDir();
usd.replace( QDir::separator(), '/' );
if( f.startsWith( usd ) )
{
return QString( f ).mid( usd.length() );
}
}
return _file;
return file;
}

View File

@@ -19,6 +19,7 @@ ADD_EXECUTABLE(tests
$<TARGET_OBJECTS:lmmsobjs>
src/core/ProjectVersionTest.cpp
src/core/RelativePathsTest.cpp
src/tracks/AutomationTrackTest.cpp
)

View File

@@ -0,0 +1,48 @@
/*
* RelativePathsTest.cpp
*
* Copyright (c) 2017 Tres Finocchiaro <tres/dot/finocchiaro/at/gmail.com>
*
* This file is part of LMMS - https://lmms.io
*
* This program 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 2 of the License, or (at your option) any later version.
*
* This program 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 this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "QTestSuite.h"
#include "ConfigManager.h"
#include "SampleBuffer.h"
#include <QDir>
class RelativePathsTest : QTestSuite
{
Q_OBJECT
private slots:
void RelativePathComparisonTests()
{
QFileInfo fi(ConfigManager::inst()->factorySamplesDir() + "/drums/kick01.ogg");
QVERIFY(fi.exists());
QString absPath = fi.absoluteFilePath();
QString relPath = "drums/kick01.ogg";
QCOMPARE(SampleBuffer::tryToMakeRelative(absPath), relPath);
QCOMPARE(SampleBuffer::tryToMakeAbsolute(relPath), absPath);
}
} RelativePathTests;
#include "RelativePathsTest.moc"