diff --git a/src/blackcore/context/contextownaircraft.cpp b/src/blackcore/context/contextownaircraft.cpp index ba363cea5..93e25432e 100644 --- a/src/blackcore/context/contextownaircraft.cpp +++ b/src/blackcore/context/contextownaircraft.cpp @@ -7,12 +7,20 @@ * contained in the LICENSE file. */ +#include "blackcore/application.h" +#include "blackcore/webdataservices.h" #include "blackcore/context/contextownaircraft.h" #include "blackcore/context/contextownaircraftempty.h" #include "blackcore/context/contextownaircraftimpl.h" #include "blackcore/context/contextownaircraftproxy.h" +#include "blackmisc/aviation/aircraftsituation.h" #include "blackmisc/dbusserver.h" +using namespace BlackMisc::Aviation; +using namespace BlackMisc::PhysicalQuantities; +using namespace BlackMisc::Geo; +using namespace BlackMisc::Simulation; + namespace BlackCore { namespace Context @@ -31,5 +39,35 @@ namespace BlackCore return new CContextOwnAircraftEmpty(parent); } } + + const BlackMisc::Aviation::CAircraftSituation &IContextOwnAircraft::getDefaultSituation() + { + static const CAircraftSituation situation( + CCoordinateGeodetic( + CLatitude::fromWgs84("N 049° 18' 17"), + CLongitude::fromWgs84("E 008° 27' 05"), + CLength(0, CLengthUnit::m())), + CAltitude(312, CAltitude::MeanSeaLevel, CLengthUnit::ft()) + ); + return situation; + } + + BlackMisc::Simulation::CAircraftModel IContextOwnAircraft::getDefaultOwnAircraftModel() + { + // if all fails + static const CAircraftModel defaultModel( + "", CAircraftModel::TypeOwnSimulatorModel, "default model", + CAircraftIcaoCode("C172", "L1P", "Cessna", "172", "L", true, false, false, 0)); + + // create one from DB data + if (sApp && sApp->hasWebDataServices()) + { + static const CAircraftIcaoCode icao = sApp->getWebDataServices()->getAircraftIcaoCodeForDesignator("C172"); + static const CLivery livery = sApp->getWebDataServices()->getLiveryForCombinedCode("_CC_WHITE_WHITE"); + static const CAircraftModel model("", CAircraftModel::TypeOwnSimulatorModel, icao, livery); + return model; + } + return defaultModel; + } } // namespace } // namespace diff --git a/src/blackcore/context/contextownaircraft.h b/src/blackcore/context/contextownaircraft.h index 7b2f793a5..c2da03763 100644 --- a/src/blackcore/context/contextownaircraft.h +++ b/src/blackcore/context/contextownaircraft.h @@ -99,7 +99,7 @@ namespace BlackCore //! Own ICAO was changed void changedAircraftIcaoCodes(const BlackMisc::Aviation::CAircraftIcaoCode &aircraftIcaoCode, const BlackMisc::Aviation::CAirlineIcaoCode &airlineIcaoCode); - //! own pilot (aka the swift user) changed + //! Own pilot (aka the swift user) changed void changedPilot(const BlackMisc::Network::CUser &pilot); public slots: @@ -142,6 +142,14 @@ namespace BlackCore //! Parse command line virtual bool parseCommandLine(const QString &commandLine, const BlackMisc::CIdentifier &originator) = 0; + //! Default situation + //! \remark normally used when no driver is attached + static const BlackMisc::Aviation::CAircraftSituation &getDefaultSituation(); + + //! Default own aircraft + //! \remark normally used when no driver is attached + static BlackMisc::Simulation::CAircraftModel getDefaultOwnAircraftModel(); + protected: //! Constructor IContextOwnAircraft(CCoreFacadeConfig::ContextMode mode, CCoreFacade *runtime) : CContext(mode, runtime) {} diff --git a/src/blackcore/context/contextownaircraftimpl.cpp b/src/blackcore/context/contextownaircraftimpl.cpp index 9c397c405..92f006a8c 100644 --- a/src/blackcore/context/contextownaircraftimpl.cpp +++ b/src/blackcore/context/contextownaircraftimpl.cpp @@ -7,6 +7,7 @@ * contained in the LICENSE file. */ +#include "blackcore/db/databaseutils.h" #include "blackcore/context/contextapplication.h" #include "blackcore/context/contextaudio.h" #include "blackcore/context/contextnetwork.h" @@ -45,6 +46,7 @@ using namespace BlackMisc::Geo; using namespace BlackMisc::Audio; using namespace BlackMisc::Simulation; using namespace BlackCore; +using namespace BlackCore::Db; namespace BlackCore { @@ -66,6 +68,8 @@ namespace BlackCore this->initOwnAircraft(); } + CContextOwnAircraft::~CContextOwnAircraft() { } + CContextOwnAircraft *CContextOwnAircraft::registerWithDBus(CDBusServer *server) { if (!server || this->m_mode != CCoreFacadeConfig::LocalInDbusServer) return this; @@ -73,8 +77,6 @@ namespace BlackCore return this; } - CContextOwnAircraft::~CContextOwnAircraft() { } - CSimulatedAircraft CContextOwnAircraft::getOwnAircraft() const { if (this->m_debugEnabled) {CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } @@ -117,20 +119,14 @@ namespace BlackCore ownAircraft.initComSystems(); ownAircraft.initTransponder(); - const CAircraftSituation situation( - CCoordinateGeodetic( - CLatitude::fromWgs84("N 049° 18' 17"), - CLongitude::fromWgs84("E 008° 27' 05"), - CLength(0, CLengthUnit::m())), - CAltitude(312, CAltitude::MeanSeaLevel, CLengthUnit::ft()) - ); - ownAircraft.setSituation(situation); - ownAircraft.setPilot(this->m_currentNetworkServer.getThreadLocal().getUser()); + ownAircraft.setSituation(getDefaultSituation()); + ownAircraft.setPilot(this->m_currentNetworkServer.get().getUser()); - // reverse lookup if possible - if (!ownAircraft.getModel().isLoadedFromDb() && !ownAircraft.getModelString().isEmpty()) + // If we already have a model from somehwere, keep it, otherwise init default + ownAircraft.setModel(this->reverseLookupModel(ownAircraft.getModel())); + if (!ownAircraft.getAircraftIcaoCode().hasValidDesignator()) { - ownAircraft.setModel(this->reverseLookupModel(ownAircraft.getModelString())); + ownAircraft.setModel(getDefaultOwnAircraftModel()); } // override empty values @@ -139,14 +135,6 @@ namespace BlackCore ownAircraft.setCallsign(CCallsign("SWIFT")); } - if (!ownAircraft.getAircraftIcaoCode().hasValidDesignator()) - { - ownAircraft.setIcaoCodes( - CAircraftIcaoCode("C172", "L1P"), - CAirlineIcaoCode() - ); - } - // update object { QWriteLocker l(&m_lockAircraft); @@ -183,34 +171,16 @@ namespace BlackCore emit this->getIContextApplication()->fakedSetComVoiceRoom(rooms); } - CAircraftModel CContextOwnAircraft::reverseLookupModel(const QString &modelString) + CAircraftModel CContextOwnAircraft::reverseLookupModel(const CAircraftModel &model) { - if (modelString.isEmpty()) { return CAircraftModel(); } - if (!sApp || !sApp->hasWebDataServices()) { return CAircraftModel(); } - if (sApp->getWebDataServices()->getModelsCount() < 1) { return CAircraftModel(); } - const CAircraftModel reverseLookupModel = sApp->getWebDataServices()->getModelForModelString(modelString); - return reverseLookupModel; + bool modified = false; + CAircraftModel reverseModel = CDatabaseUtils::consolidateOwnAircraftModelWithDbData(model, false, &modified); + return reverseModel; } bool CContextOwnAircraft::updateOwnModel(const CAircraftModel &model) { - // reverse lookup if not yet from DB: - // this is the central place where we keep our own model, so we use best effort - // to make that model as accurate as we can - CAircraftModel updateModel(model); - if (!updateModel.isLoadedFromDb()) - { - CAircraftModel reverseModel = reverseLookupModel(model.getModelString()); - if (reverseModel.isLoadedFromDb()) - { - // special case here, as we have some specific values for a local model - updateModel = reverseModel; - updateModel.updateMissingParts(model); - updateModel.setFileName(model.getFileName()); - } - } - - updateModel.setModelType(CAircraftModel::TypeOwnSimulatorModel); + CAircraftModel updateModel(this->reverseLookupModel(model)); QWriteLocker l(&m_lockAircraft); const bool changed = (this->m_ownAircraft.getModel() != updateModel); if (!changed) { return false; } diff --git a/src/blackcore/context/contextownaircraftimpl.h b/src/blackcore/context/contextownaircraftimpl.h index ce56a575b..9529e5604 100644 --- a/src/blackcore/context/contextownaircraftimpl.h +++ b/src/blackcore/context/contextownaircraftimpl.h @@ -182,8 +182,8 @@ namespace BlackCore //! Resolve voice rooms void resolveVoiceRooms(); - //! Reverse lookup of the model - static BlackMisc::Simulation::CAircraftModel reverseLookupModel(const QString &modelString); + //! Reverse lookup of the model against DB data + static BlackMisc::Simulation::CAircraftModel reverseLookupModel(const BlackMisc::Simulation::CAircraftModel &model); }; } // ns } // ns diff --git a/src/blackgui/components/logincomponent.cpp b/src/blackgui/components/logincomponent.cpp index bcdd9b6d4..42461a9ad 100644 --- a/src/blackgui/components/logincomponent.cpp +++ b/src/blackgui/components/logincomponent.cpp @@ -672,26 +672,9 @@ namespace BlackGui CAircraftModel CLoginComponent::getPrefillModel() const { - // if all fails - static const CAircraftModel defaultModel( - "", CAircraftModel::TypeOwnSimulatorModel, "default model", - CAircraftIcaoCode("C172", "L1P", "Cessna", "172", "L", true, false, false, 0)); - CAircraftModel model = this->m_currentAircraftModel.get(); if (model.hasAircraftDesignator()) { return model; } - - // create one from DB data - if (sGui && sGui->hasWebDataServices()) - { - const CAircraftIcaoCode icao = sGui->getWebDataServices()->getAircraftIcaoCodeForDesignator("C172"); - const CLivery livery = sGui->getWebDataServices()->getLiveryForCombinedCode("_CC_WHITE_WHITE"); - model = CAircraftModel("", CAircraftModel::TypeOwnSimulatorModel); - model.setLivery(livery); - model.setAircraftIcaoCode(icao); - return model; - } - - return defaultModel; + return IContextOwnAircraft::getDefaultOwnAircraftModel(); } void CLoginComponent::updateOwnCallsignAndPilotFromGuiValue()