Skip to content

Commit

Permalink
Make factory samples relative (LMMS#3510)
Browse files Browse the repository at this point in the history
* Make factory samples relative
Fixes LMMS#3491
Related LMMS#1719
  • Loading branch information
tresf authored and lukas-w committed May 6, 2017
1 parent 6ff11f9 commit e89d13f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/core/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 21 additions & 11 deletions src/core/SampleBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ADD_EXECUTABLE(tests
$<TARGET_OBJECTS:lmmsobjs>

src/core/ProjectVersionTest.cpp
src/core/RelativePathsTest.cpp

src/tracks/AutomationTrackTest.cpp
)
Expand Down
48 changes: 48 additions & 0 deletions tests/src/core/RelativePathsTest.cpp
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e89d13f

Please sign in to comment.