refs #868, added xplaneDir utility function to remove boilerplate code

and using CFileUtils::appendFilePaths to concat file paths
This commit is contained in:
Klaus Basan
2017-03-07 19:33:31 +01:00
committed by Mathew Sutcliffe
parent 99c792dd4b
commit 0851b8c994
2 changed files with 30 additions and 34 deletions

View File

@@ -8,8 +8,8 @@
*/ */
#include "blackmisc/simulation/xplane/xplaneutil.h" #include "blackmisc/simulation/xplane/xplaneutil.h"
#include "blackmisc/fileutils.h"
#include "qsystemdetection.h" #include "qsystemdetection.h"
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
@@ -28,11 +28,10 @@ namespace BlackMisc
{ {
namespace XPlane namespace XPlane
{ {
// Returns the last path from filePath, which does exist on the file system // Returns the last path from filePath, which does exist on the file system
QString getLastExistingPathFromFile(const QString &filePath) QString getLastExistingPathFromFile(const QString &filePath)
{ {
QFileInfo fileInfo(filePath); const QFileInfo fileInfo(filePath);
if (!fileInfo.exists()) { return {}; } if (!fileInfo.exists()) { return {}; }
QFile file(fileInfo.absoluteFilePath()); QFile file(fileInfo.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly)) if (!file.open(QIODevice::ReadOnly))
@@ -69,46 +68,38 @@ namespace BlackMisc
QString CXPlaneUtil::xplane9Dir() QString CXPlaneUtil::xplane9Dir()
{ {
QString xplaneInstallFilePath; static const QString xplaneInstallFile("x-plane_install.txt");
QString xplaneInstallFile = QStringLiteral("/x-plane_install.txt"); const QString xplaneInstallFilePath = xplaneDir(xplaneInstallFile);
#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
return getLastExistingPathFromFile(xplaneInstallFilePath); return getLastExistingPathFromFile(xplaneInstallFilePath);
} }
QString CXPlaneUtil::xplane10Dir() QString CXPlaneUtil::xplane10Dir()
{ {
QString xplaneInstallFilePath; static const QString xplaneInstallFile("x-plane_install_10.txt");
QString xplaneInstallFile = QStringLiteral("/x-plane_install_10.txt"); const QString xplaneInstallFilePath = xplaneDir(xplaneInstallFile);
#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
return getLastExistingPathFromFile(xplaneInstallFilePath); return getLastExistingPathFromFile(xplaneInstallFilePath);
} }
QString CXPlaneUtil::xplane11Dir() QString CXPlaneUtil::xplane11Dir()
{ {
QString xplaneInstallFilePath; static const QString xplaneInstallFile("x-plane_install_11.txt");
QString xplaneInstallFile = QStringLiteral("/x-plane_install_11.txt"); const QString xplaneInstallFilePath = xplaneDir(xplaneInstallFile);
#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
return getLastExistingPathFromFile(xplaneInstallFilePath); 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() QString CXPlaneUtil::xplaneRootDir()
{ {
if (!xplane11Dir().isEmpty()) { return xplane11Dir(); } if (!xplane11Dir().isEmpty()) { return xplane11Dir(); }
@@ -130,19 +121,20 @@ namespace BlackMisc
QString CXPlaneUtil::xbusLegacyDir(const QString &rootDir) 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. // Return the first non empty path, we can find.
if (!rootDir.isEmpty()) if (!rootDir.isEmpty())
{ {
QString xbusLegacy = rootDir + legacyPath; const QString xbusLegacy = CFileUtils::appendFilePaths(rootDir, legacyPath);
if (QDir(xbusLegacy).exists()) if (QDir(xbusLegacy).exists())
{ {
return xbusLegacy; return xbusLegacy;
} }
} }
for (auto func: {&CXPlaneUtil::xplane11Dir, &CXPlaneUtil::xplane10Dir, &CXPlaneUtil::xplane9Dir}) { for (auto func : {&CXPlaneUtil::xplane11Dir, &CXPlaneUtil::xplane10Dir, &CXPlaneUtil::xplane9Dir})
QString xbusLegacy = func() + legacyPath; {
const QString xbusLegacy = CFileUtils::appendFilePaths(func(), legacyPath);
if (QDir(xbusLegacy).exists()) if (QDir(xbusLegacy).exists())
{ {
return xbusLegacy; return xbusLegacy;

View File

@@ -50,6 +50,10 @@ namespace BlackMisc
//! XBus legacy directory //! XBus legacy directory
static QString xbusLegacyDir(const QString &rootDir); static QString xbusLegacyDir(const QString &rootDir);
private:
//! Concatenates dirs for used OS
static QString xplaneDir(const QString &xplaneInstallFile);
}; };
} // namespace } // namespace
} // namespace } // namespace