diff --git a/samples/blacksim/samplesmodelmapping.cpp b/samples/blacksim/samplesmodelmapping.cpp index 91ce0fe1d..b028d3046 100644 --- a/samples/blacksim/samplesmodelmapping.cpp +++ b/samples/blacksim/samplesmodelmapping.cpp @@ -6,11 +6,11 @@ #include "samplesmodelmapping.h" #include "blackmisc/blackmiscfreefunctions.h" #include "blacksim/blacksimfreefunctions.h" -#include "blacksim/vpilotmodelmappings.h" +#include "blacksim/fscommon/vpilotmodelmappings.h" #include -using namespace BlackSim; +using namespace BlackSim::FsCommon; namespace BlackSimTest { diff --git a/src/blacksim/fscommon/vpilotmodelmappings.cpp b/src/blacksim/fscommon/vpilotmodelmappings.cpp new file mode 100644 index 000000000..eeaca9553 --- /dev/null +++ b/src/blacksim/fscommon/vpilotmodelmappings.cpp @@ -0,0 +1,98 @@ +#include "vpilotmodelmappings.h" +#include "blackmisc/nwaircraftmapping.h" + +#include +#include +#include +#include + +using namespace BlackMisc::Network; + +namespace BlackSim +{ + namespace FsCommon + { + + void CVPilotModelMappings::addFilename(const QString &fileName) + { + if (this->m_fileList.contains(fileName)) return; + this->m_fileList.append(fileName); + } + + void CVPilotModelMappings::addDirectory(const QString &directory) + { + QDir dir(directory); + if (!dir.exists()) return; + QStringList nameFilters({"*.vmr"}); + QFileInfoList entries = dir.entryInfoList(nameFilters, QDir::Files | QDir::Readable); + foreach(QFileInfo file, entries) + { + this->addFilename(file.absoluteFilePath()); + } + } + + const QString &CVPilotModelMappings::standardMappingsDirectory() + { + static QString directory; + if (directory.isEmpty()) + { + directory = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first(); + if (!directory.endsWith('/')) directory.append('/'); + directory.append("vPilot Files/Model Matching Rule Sets"); + } + return directory; + } + + bool CVPilotModelMappings::load() + { + bool success = true; + foreach(QString fn, this->m_fileList) + { + bool s = this->loadFile(fn); + success = s && success; + } + return success; + } + + bool CVPilotModelMappings::loadFile(const QString &fileName) + { + QFile f(fileName); + if (!f.exists()) return false; + if (!f.open(QFile::ReadOnly | QFile::Text)) return false; + QByteArray fc = f.readAll(); + if (fc.isEmpty()) return false; + QDomDocument doc; + if (!doc.setContent(fc)) return false; + QDomNodeList rules = doc.elementsByTagName("ModelMatchRule"); + if (rules.isEmpty()) return false; + int size = rules.size(); + for (int i = 0; i < size; i++) + { + QDomNamedNodeMap attributes = rules.at(i).attributes(); + const QString typeCode = attributes.namedItem("TypeCode").nodeValue(); + const QString modelName = attributes.namedItem("ModelName").nodeValue(); + const QString callsignPrefix = attributes.namedItem("CallsignPrefix").nodeValue(); + if (modelName.isEmpty()) continue; + + if (modelName.contains("//")) + { + // multiple models + QStringList models = modelName.split("//"); + foreach(QString model, models) + { + if (model.isEmpty()) continue; + CAircraftMapping mapping(typeCode, callsignPrefix, model); + this->m_mappings.push_back(mapping); + } + } + else + { + // single model + CAircraftMapping mapping(typeCode, callsignPrefix, modelName); + this->m_mappings.push_back(mapping); + } + } + return true; + } + } // namespace +} // namespace diff --git a/src/blacksim/fscommon/vpilotmodelmappings.h b/src/blacksim/fscommon/vpilotmodelmappings.h new file mode 100644 index 000000000..cc508887c --- /dev/null +++ b/src/blacksim/fscommon/vpilotmodelmappings.h @@ -0,0 +1,49 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKSIM_FSCOMMON_VPILOTMODELMAPPINGS_H +#define BLACKSIM_FSCOMMON_VPILOTMODELMAPPINGS_H + +#include "../simulatormodelmappings.h" +#include + +namespace BlackSim +{ + namespace FsCommon + { + /*! + * Model mappings + */ + class CVPilotModelMappings : public ISimulatorModelMappings + { + public: + //! Constructor + CVPilotModelMappings(QObject *parent = nullptr) : ISimulatorModelMappings(parent) {} + + //! Destructor + virtual ~CVPilotModelMappings() {} + + //! File names + void addFilename(const QString &fileName); + + //! Directory with .vmr files + void addDirectory(const QString &directory); + + //! The standard directory for vPilot mappings + static const QString &standardMappingsDirectory(); + + public slots: + //! Load data + virtual bool load() override; + + private: + QStringList m_fileList; //!< list of file names + + //! Single file read and parsing + bool loadFile(const QString &fileName); + }; + } // namespace +} // namespace +#endif // guard diff --git a/src/blacksim/vpilotmodelmappings.cpp b/src/blacksim/vpilotmodelmappings.cpp deleted file mode 100644 index 165c3d6b5..000000000 --- a/src/blacksim/vpilotmodelmappings.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "vpilotmodelmappings.h" -#include "blackmisc/nwaircraftmapping.h" - -#include -#include -#include -#include - -using namespace BlackMisc::Network; - -namespace BlackSim -{ - - void CVPilotModelMappings::addFilename(const QString &fileName) - { - if (this->m_fileList.contains(fileName)) return; - this->m_fileList.append(fileName); - } - - void CVPilotModelMappings::addDirectory(const QString &directory) - { - QDir dir(directory); - if (!dir.exists()) return; - QStringList nameFilters({"*.vmr"}); - QFileInfoList entries = dir.entryInfoList(nameFilters, QDir::Files | QDir::Readable); - foreach(QFileInfo file, entries) - { - this->addFilename(file.absoluteFilePath()); - } - } - - const QString &CVPilotModelMappings::standardMappingsDirectory() - { - static QString directory; - if (directory.isEmpty()) - { - directory = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first(); - if (!directory.endsWith('/')) directory.append('/'); - directory.append("vPilot Files/Model Matching Rule Sets"); - } - return directory; - } - - bool CVPilotModelMappings::load() - { - bool success = true; - foreach(QString fn, this->m_fileList) - { - bool s = this->loadFile(fn); - success = s && success; - } - return success; - } - - bool CVPilotModelMappings::loadFile(const QString &fileName) - { - QFile f(fileName); - if (!f.exists()) return false; - if (!f.open(QFile::ReadOnly | QFile::Text)) return false; - QByteArray fc = f.readAll(); - if (fc.isEmpty()) return false; - QDomDocument doc; - if (!doc.setContent(fc)) return false; - QDomNodeList rules = doc.elementsByTagName("ModelMatchRule"); - if (rules.isEmpty()) return false; - int size = rules.size(); - for (int i = 0; i < size; i++) - { - QDomNamedNodeMap attributes = rules.at(i).attributes(); - const QString typeCode = attributes.namedItem("TypeCode").nodeValue(); - const QString modelName = attributes.namedItem("ModelName").nodeValue(); - const QString callsignPrefix = attributes.namedItem("CallsignPrefix").nodeValue(); - if (modelName.isEmpty()) continue; - - if (modelName.contains("//")) - { - // multiple models - QStringList models = modelName.split("//"); - foreach(QString model, models) - { - if (model.isEmpty()) continue; - CAircraftMapping mapping(typeCode, callsignPrefix, model); - this->m_mappings.push_back(mapping); - } - } - else - { - // single model - CAircraftMapping mapping(typeCode, callsignPrefix, modelName); - this->m_mappings.push_back(mapping); - } - } - return true; - } -} // namespace diff --git a/src/blacksim/vpilotmodelmappings.h b/src/blacksim/vpilotmodelmappings.h deleted file mode 100644 index f8e5c11d9..000000000 --- a/src/blacksim/vpilotmodelmappings.h +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright (C) 2013 VATSIM Community / authors - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef BLACKSIM_VPILOTMODELMAPPINGS_H -#define BLACKSIM_VPILOTMODELMAPPINGS_H - -#include "simulatormodelmappings.h" -#include - -namespace BlackSim -{ - /*! - * \brief Model mappings - */ - class CVPilotModelMappings : public ISimulatorModelMappings - { - - protected: - - public: - //! Constructor - CVPilotModelMappings(QObject *parent = nullptr) : ISimulatorModelMappings(parent) {} - - //! Destructor - virtual ~CVPilotModelMappings() {} - - //! File names - void addFilename(const QString &fileName); - - //! Directory with .vmr files - void addDirectory(const QString &directory); - - //! The standard directory for vPilot mappings - static const QString &standardMappingsDirectory(); - - public slots: - //! Load data - virtual bool load() override; - - private: - QStringList m_fileList; //!< list of file names - - //! Single file read and parsing - bool loadFile(const QString &fileName); - }; -} // namespace -#endif // guard