From e89d13fdc51eaef89449e1f23d2e269f217e900f Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Sat, 6 May 2017 04:27:18 -0400 Subject: [PATCH] Make factory samples relative (#3510) * Make factory samples relative Fixes #3491 Related #1719 --- src/core/ConfigManager.cpp | 7 +++- src/core/SampleBuffer.cpp | 32 ++++++++++++------- tests/CMakeLists.txt | 1 + tests/src/core/RelativePathsTest.cpp | 48 ++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 tests/src/core/RelativePathsTest.cpp diff --git a/src/core/ConfigManager.cpp b/src/core/ConfigManager.cpp index 5cdfe3d97a4..13c6d9a85b3 100644 --- a/src/core/ConfigManager.cpp +++ b/src/core/ConfigManager.cpp @@ -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); diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index f5ec42cdbac..cb930b08789 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -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; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2367ea4bbc2..8cbe23858bf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,6 +19,7 @@ ADD_EXECUTABLE(tests $ src/core/ProjectVersionTest.cpp + src/core/RelativePathsTest.cpp src/tracks/AutomationTrackTest.cpp ) diff --git a/tests/src/core/RelativePathsTest.cpp b/tests/src/core/RelativePathsTest.cpp new file mode 100644 index 00000000000..555fa39b56a --- /dev/null +++ b/tests/src/core/RelativePathsTest.cpp @@ -0,0 +1,48 @@ +/* + * RelativePathsTest.cpp + * + * Copyright (c) 2017 Tres Finocchiaro + * + * 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 + +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"