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.
This commit is contained in:
Vasily
2019-03-19 16:10:35 +01:00
committed by username223
parent d6660049e9
commit 62dcea6943
3 changed files with 24 additions and 37 deletions
+13 -4
View File
@@ -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 )
{
+10 -24
View File
@@ -21,8 +21,10 @@ optional<string> binaryDirectory()
optional<string> 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<string> binaryDirectoryFindFile( const string fileName )
return path.toStdString();
}
optional<string> qmlDirectory()
optional<string> 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<string> 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
+1 -9
View File
@@ -9,15 +9,7 @@ using std::string;
optional<string> binaryDirectory();
optional<string> binaryDirectoryFindFile( string fileName );
optional<string> qmlDirectory();
optional<string> qmlDirectoryFindFile( string fileName );
optional<string> imageDirectory();
optional<string> imageDirectoryFindFile( string fileName );
optional<string> soundDirectory();
optional<string> soundDirectoryFindFile( string fileName );
optional<string> binaryDirectoryFindDirectory( string directoryName );
optional<string> settingsDirectory();
optional<string> settingsDirectoryFindFile( string fileName );