From 0851b8c99482a405aa840bb131016e80e4f35825 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 7 Mar 2017 19:33:31 +0100 Subject: [PATCH] refs #868, added xplaneDir utility function to remove boilerplate code and using CFileUtils::appendFilePaths to concat file paths --- .../simulation/xplane/xplaneutil.cpp | 60 ++++++++----------- src/blackmisc/simulation/xplane/xplaneutil.h | 4 ++ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/blackmisc/simulation/xplane/xplaneutil.cpp b/src/blackmisc/simulation/xplane/xplaneutil.cpp index ffb3ad5cc..cd5601cbb 100644 --- a/src/blackmisc/simulation/xplane/xplaneutil.cpp +++ b/src/blackmisc/simulation/xplane/xplaneutil.cpp @@ -8,8 +8,8 @@ */ #include "blackmisc/simulation/xplane/xplaneutil.h" +#include "blackmisc/fileutils.h" #include "qsystemdetection.h" - #include #include #include @@ -28,11 +28,10 @@ namespace BlackMisc { namespace XPlane { - // Returns the last path from filePath, which does exist on the file system QString getLastExistingPathFromFile(const QString &filePath) { - QFileInfo fileInfo(filePath); + const QFileInfo fileInfo(filePath); if (!fileInfo.exists()) { return {}; } QFile file(fileInfo.absoluteFilePath()); if (!file.open(QIODevice::ReadOnly)) @@ -69,46 +68,38 @@ namespace BlackMisc QString CXPlaneUtil::xplane9Dir() { - QString xplaneInstallFilePath; - QString xplaneInstallFile = QStringLiteral("/x-plane_install.txt"); -#if defined(Q_OS_WIN) - xplaneInstallFilePath = getWindowsLocalAppDataPath() + xplaneInstallFile; -#elif defined (Q_OS_LINUX) - xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/.x-plane") + xplaneInstallFile; -#elif defined (Q_OS_OSX) - xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/Library/Preferences") + xplaneInstallFile; -#endif + static const QString xplaneInstallFile("x-plane_install.txt"); + const QString xplaneInstallFilePath = xplaneDir(xplaneInstallFile); return getLastExistingPathFromFile(xplaneInstallFilePath); } QString CXPlaneUtil::xplane10Dir() { - QString xplaneInstallFilePath; - QString xplaneInstallFile = QStringLiteral("/x-plane_install_10.txt"); -#if defined(Q_OS_WIN) - xplaneInstallFilePath = getWindowsLocalAppDataPath() + xplaneInstallFile; -#elif defined (Q_OS_LINUX) - xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/.x-plane") + xplaneInstallFile; -#elif defined (Q_OS_OSX) - xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/Library/Preferences") + xplaneInstallFile; -#endif + static const QString xplaneInstallFile("x-plane_install_10.txt"); + const QString xplaneInstallFilePath = xplaneDir(xplaneInstallFile); return getLastExistingPathFromFile(xplaneInstallFilePath); } QString CXPlaneUtil::xplane11Dir() { - QString xplaneInstallFilePath; - QString xplaneInstallFile = QStringLiteral("/x-plane_install_11.txt"); -#if defined(Q_OS_WIN) - xplaneInstallFilePath = getWindowsLocalAppDataPath() + xplaneInstallFile; -#elif defined (Q_OS_LINUX) - xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/.x-plane") + xplaneInstallFile; -#elif defined (Q_OS_OSX) - xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/Library/Preferences") + xplaneInstallFile; -#endif + static const QString xplaneInstallFile("x-plane_install_11.txt"); + const QString xplaneInstallFilePath = xplaneDir(xplaneInstallFile); return getLastExistingPathFromFile(xplaneInstallFilePath); } + QString CXPlaneUtil::xplaneDir(const QString &xplaneInstallFile) + { +#if defined(Q_OS_WIN) + return CFileUtils::appendFilePaths(getWindowsLocalAppDataPath(), xplaneInstallFile); +#elif defined (Q_OS_LINUX) + static const QString xp(".x-plane"); + return CFileUtils::appendFilePaths(QDir::homePath(), xp, xplaneInstallFile); +#elif defined (Q_OS_OSX) + static const QString lib("Library/Preferences"); + return CFileUtils::appendFilePaths(QDir::homePath(), lib, xplaneInstallFile); +#endif + } + QString CXPlaneUtil::xplaneRootDir() { if (!xplane11Dir().isEmpty()) { return xplane11Dir(); } @@ -130,19 +121,20 @@ namespace BlackMisc QString CXPlaneUtil::xbusLegacyDir(const QString &rootDir) { - QString legacyPath("/Resources/plugins/xbus/LegacyData"); + static const QString legacyPath("/Resources/plugins/xbus/LegacyData"); // Return the first non empty path, we can find. if (!rootDir.isEmpty()) { - QString xbusLegacy = rootDir + legacyPath; + const QString xbusLegacy = CFileUtils::appendFilePaths(rootDir, legacyPath); if (QDir(xbusLegacy).exists()) { return xbusLegacy; } } - for (auto func: {&CXPlaneUtil::xplane11Dir, &CXPlaneUtil::xplane10Dir, &CXPlaneUtil::xplane9Dir}) { - QString xbusLegacy = func() + legacyPath; + for (auto func : {&CXPlaneUtil::xplane11Dir, &CXPlaneUtil::xplane10Dir, &CXPlaneUtil::xplane9Dir}) + { + const QString xbusLegacy = CFileUtils::appendFilePaths(func(), legacyPath); if (QDir(xbusLegacy).exists()) { return xbusLegacy; diff --git a/src/blackmisc/simulation/xplane/xplaneutil.h b/src/blackmisc/simulation/xplane/xplaneutil.h index 3619d77ed..1719fb887 100644 --- a/src/blackmisc/simulation/xplane/xplaneutil.h +++ b/src/blackmisc/simulation/xplane/xplaneutil.h @@ -50,6 +50,10 @@ namespace BlackMisc //! XBus legacy directory static QString xbusLegacyDir(const QString &rootDir); + + private: + //! Concatenates dirs for used OS + static QString xplaneDir(const QString &xplaneInstallFile); }; } // namespace } // namespace