From 35cb4e20679f2f33f18a2a2712161b4ec66061f2 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Tue, 6 Oct 2015 22:54:38 +0200 Subject: [PATCH] refs #487 add XPlane path utilities --- mkspecs/features/libraries.pri | 4 + src/blackmisc/blackmisc.pro | 6 + .../simulation/xplane/xplaneutil.cpp | 120 ++++++++++++++++++ src/blackmisc/simulation/xplane/xplaneutil.h | 47 +++++++ 4 files changed, 177 insertions(+) create mode 100644 src/blackmisc/simulation/xplane/xplaneutil.cpp create mode 100644 src/blackmisc/simulation/xplane/xplaneutil.h diff --git a/mkspecs/features/libraries.pri b/mkspecs/features/libraries.pri index f3d0d7d2a..e09ad6453 100644 --- a/mkspecs/features/libraries.pri +++ b/mkspecs/features/libraries.pri @@ -78,4 +78,8 @@ blackmisc { macx-clang: PRE_TARGETDEPS += $$DestRoot/lib/libblackmisc.dylib } LIBS *= -lblackmisc + + win32 { + LIBS *= -lShell32 + } } diff --git a/src/blackmisc/blackmisc.pro b/src/blackmisc/blackmisc.pro index bd2f1ba4b..901fb9e2a 100644 --- a/src/blackmisc/blackmisc.pro +++ b/src/blackmisc/blackmisc.pro @@ -37,6 +37,7 @@ HEADERS += *.h \ $$PWD/simulation/*.h \ $$PWD/simulation/fscommon/*.h \ $$PWD/simulation/fsx/*.h \ + $$PWD/simulation/xplane/*.h \ $$PWD/weather/*.h SOURCES += *.cpp \ @@ -50,8 +51,13 @@ SOURCES += *.cpp \ $$PWD/simulation/*.cpp \ $$PWD/simulation/fscommon/*.cpp \ $$PWD/simulation/fsx/*.cpp \ + $$PWD/simulation/xplane/*.cpp \ $$PWD/weather/*.cpp +win32 { + LIBS *= -lShell32 +} + DESTDIR = $$DestRoot/lib DLLDESTDIR = $$DestRoot/bin diff --git a/src/blackmisc/simulation/xplane/xplaneutil.cpp b/src/blackmisc/simulation/xplane/xplaneutil.cpp new file mode 100644 index 000000000..8bf9cf8ab --- /dev/null +++ b/src/blackmisc/simulation/xplane/xplaneutil.cpp @@ -0,0 +1,120 @@ +/* Copyright (C) 2015 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "xplaneutil.h" +#include +#include +#include + +#if defined(Q_OS_WIN) +#include +#endif + +using namespace BlackMisc; + +namespace BlackMisc +{ + namespace Simulation + { + namespace XPlane + { + + // Returns the last path from filePath, which does exist on the file system + QString getLastExistingPathFromFile(const QString &filePath) + { + QFileInfo fileInfo(filePath); + if (!fileInfo.exists()) { return {}; } + QFile file(fileInfo.absoluteFilePath()); + if (!file.open(QIODevice::ReadOnly)) + { + return {}; + } + + QString lastLine; + QTextStream ts(&file); + while (!ts.atEnd()) + { + QString pathToCheck = ts.readLine(); + if (QFileInfo(pathToCheck).exists()) { lastLine = pathToCheck; } + } + return lastLine; + } + +#if defined(Q_OS_WIN) + QString getWindowsLocalAppDataPath() + { + QString result; + TCHAR szLocalAppDataPath[MAX_PATH]; + if(SUCCEEDED(SHGetFolderPath(NULL, + CSIDL_LOCAL_APPDATA, + NULL, + 0, + szLocalAppDataPath))) + { + result = QString::fromWCharArray(szLocalAppDataPath); + } + return result; + } +#endif + + 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 + 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_UNIX) + xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/.x-plane") + xplaneInstallFile; +#elif defined (Q_OS_OSX) + xplaneInstallFilePath = QDir::homePath() + QStringLiteral("/Library/Preferences") + xplaneInstallFile; +#endif + return getLastExistingPathFromFile(xplaneInstallFilePath); + } + + QString CXPlaneUtil::xbusLegacyDir() + { + QString legacyPath("/Resources/plugins/xbus/LegacyData"); + // Return the first non empty path, we can find. + if (!xplane10Dir().isEmpty()) + { + QString xbusLegacy = xplane10Dir() + legacyPath; + if (QDir(xbusLegacy).exists()) + { + return xbusLegacy; + } + } + + if (!xplane9Dir().isEmpty()) + { + QString xbusLegacy = xplane9Dir() + legacyPath; + if (QDir(xbusLegacy).exists()) + { + return xbusLegacy; + } + } + return {}; + } + + } // namespace + } // namespace +} // namespace diff --git a/src/blackmisc/simulation/xplane/xplaneutil.h b/src/blackmisc/simulation/xplane/xplaneutil.h new file mode 100644 index 000000000..2b567698b --- /dev/null +++ b/src/blackmisc/simulation/xplane/xplaneutil.h @@ -0,0 +1,47 @@ +/* Copyright (C) 2015 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_SIMULATION_XPLANE_XPLANEUTIL_H +#define BLACKMISC_SIMULATION_XPLANE_XPLANEUTIL_H + +#include "blackmisc/blackmiscexport.h" +#include + +namespace BlackMisc +{ + namespace Simulation + { + namespace XPlane + { + //! XPlane utils + class BLACKMISC_EXPORT CXPlaneUtil + { + public: + //! Constructor + CXPlaneUtil() = delete; + + //! XPlane 9 directory + //! \todo Test on OSX + static QString xplane9Dir(); + + //! XPlane 10 directory + //! \todo Test on OSX + static QString xplane10Dir(); + + //! XBus legacy directory + static QString xbusLegacyDir(); + }; + + } // namespace + } // namespace +} // namespace + +#endif // guard