From 62dcea694307fab90135d0fbcdac39ff6a2067e5 Mon Sep 17 00:00:00 2001 From: Vasily Date: Sun, 17 Mar 2019 16:18:06 +0100 Subject: [PATCH] Replace dedicated functions with single binary function There are only 1 or 2 calls to specific directories in the codebase, hence this is unnecessary and just leads to bloat. --- src/main.cpp | 17 +++++++++++++---- src/utils/paths.cpp | 34 ++++++++++------------------------ src/utils/paths.h | 10 +--------- 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f76044b..cbc8c03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,11 +48,20 @@ int main( int argc, char* argv[] ) commandLineArgs.forceNoSound, qmlEngine ); - QString path = QStandardPaths::locate( - QStandardPaths::AppDataLocation, - QStringLiteral( "res/qml/common/mainwidget.qml" ) ); + const auto path + = paths::binaryDirectoryFindFile( "res/qml/common/mainwidget.qml" ); - QQmlComponent component( &qmlEngine, QUrl::fromLocalFile( path ) ); + if ( !path.has_value() ) + { + LOG( ERROR ) << "Unable to find file '" << *path << "'."; + throw std::runtime_error( + "Unable to find critical file. See log for more information." ); + } + + const auto url + = QUrl::fromLocalFile( QString::fromStdString( ( *path ) ) ); + + QQmlComponent component( &qmlEngine, url ); auto errors = component.errors(); for ( auto& e : errors ) { diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp index 79ed8bc..7dadca0 100644 --- a/src/utils/paths.cpp +++ b/src/utils/paths.cpp @@ -21,8 +21,10 @@ optional binaryDirectory() optional binaryDirectoryFindFile( const string fileName ) { - const auto path = QStandardPaths::locate( - QStandardPaths::AppDataLocation, QString::fromStdString( fileName ) ); + const auto path + = QStandardPaths::locate( QStandardPaths::AppDataLocation, + QString::fromStdString( fileName ), + QStandardPaths::LocateFile ); if ( path == "" ) { LOG( ERROR ) << "Could not find file '" << fileName.c_str() @@ -33,36 +35,20 @@ optional binaryDirectoryFindFile( const string fileName ) return path.toStdString(); } -optional qmlDirectory() +optional binaryDirectoryFindDirectory( string directoryName ) { const auto path - = QStandardPaths::locate( QStandardPaths::AppDataLocation, "res/qml/" ); + = QStandardPaths::locate( QStandardPaths::AppDataLocation, + QString::fromStdString( directoryName ), + QStandardPaths::LocateDirectory ); if ( path == "" ) { - LOG( ERROR ) << "Unable to find QML directory."; + LOG( ERROR ) << "Could not find directory '" << directoryName.c_str() + << "' in binary directory."; return std::nullopt; } return path.toStdString(); } -optional qmlDirectoryFindFile( string fileName ) -{ - const auto qmlPath = qmlDirectory(); - if ( !qmlPath.has_value() ) - { - return std::nullopt; - } - - const auto path = ( *qmlPath ) + fileName; - QFileInfo filePath( QString::fromStdString( path ) ); - if ( !filePath.exists() ) - { - LOG( ERROR ) << "File: '" << path << "' not found."; - return std::nullopt; - } - - return std::move( path ); -} - } // namespace paths diff --git a/src/utils/paths.h b/src/utils/paths.h index 7ce5685..926c752 100644 --- a/src/utils/paths.h +++ b/src/utils/paths.h @@ -9,15 +9,7 @@ using std::string; optional binaryDirectory(); optional binaryDirectoryFindFile( string fileName ); - -optional qmlDirectory(); -optional qmlDirectoryFindFile( string fileName ); - -optional imageDirectory(); -optional imageDirectoryFindFile( string fileName ); - -optional soundDirectory(); -optional soundDirectoryFindFile( string fileName ); +optional binaryDirectoryFindDirectory( string directoryName ); optional settingsDirectory(); optional settingsDirectoryFindFile( string fileName );