diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 4c09748a2..51493d18c 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -133,7 +133,7 @@ namespace BlackCore if (withMetadata) { CApplication::registerMetadata(); } // unit test - if (this->getApplicationInfo().application() == CApplicationInfo::UnitTest) + if (this->getApplicationInfo().getApplication() == CApplicationInfo::UnitTest) { const QString tempPath(this->getTemporaryDirectory()); BlackMisc::setMockCacheRootDirectory(tempPath); @@ -251,7 +251,7 @@ namespace BlackCore bool CApplication::isAlreadyRunning() const { - return getRunningApplications().containsBy([this](const CApplicationInfo & info) { return info.application() == getSwiftApplication(); }); + return getRunningApplications().containsBy([this](const CApplicationInfo & info) { return info.getApplication() == getSwiftApplication(); }); } bool CApplication::isShuttingDown() const diff --git a/src/blackcore/application.h b/src/blackcore/application.h index abf7fbb13..7c16d97bf 100644 --- a/src/blackcore/application.h +++ b/src/blackcore/application.h @@ -143,7 +143,7 @@ namespace BlackCore void setSingleApplication(bool singleApplication); //! swift application running - BlackMisc::CApplicationInfo::Application getSwiftApplication() const { return m_applicationInfo.application(); } + BlackMisc::CApplicationInfo::Application getSwiftApplication() const { return m_applicationInfo.getApplication(); } //! Executable names for the given applications QString getExecutableForApplication(BlackMisc::CApplicationInfo::Application application) const; diff --git a/src/blackgui/guiapplication.cpp b/src/blackgui/guiapplication.cpp index cbe2bf5ca..039e25e2d 100644 --- a/src/blackgui/guiapplication.cpp +++ b/src/blackgui/guiapplication.cpp @@ -550,7 +550,7 @@ namespace BlackGui addMenuForStyleSheets(menu); QAction *a = nullptr; bool c = false; - if (this->getApplicationInfo().application() != CApplicationInfo::Laucher) + if (this->getApplicationInfo().getApplication() != CApplicationInfo::Laucher) { menu.addSeparator(); a = menu.addAction(CIcons::swiftLauncher24(), "Start swift launcher"); diff --git a/src/blackmisc/applicationinfo.cpp b/src/blackmisc/applicationinfo.cpp index 26e2c5db9..d5b418959 100644 --- a/src/blackmisc/applicationinfo.cpp +++ b/src/blackmisc/applicationinfo.cpp @@ -9,8 +9,11 @@ //! \file -#include "blackconfig/buildconfig.h" #include "blackmisc/applicationinfo.h" +#include "blackmisc/iconlist.h" +#include "blackmisc/comparefunctions.h" +#include "blackconfig/buildconfig.h" +#include using namespace BlackConfig; @@ -33,26 +36,128 @@ namespace BlackMisc } } + const QString &CApplicationInfo::getApplicationAsString() const + { + static const QString unknown("unknown"); + static const QString launcher("launcher"); + static const QString core("core"); + static const QString gui("gui"); + static const QString mapping("mapping tool"); + static const QString unitTest("unit test"); + static const QString sample("sample"); + + switch (getApplication()) + { + case Laucher: return launcher; + case PilotClientCore: return core; + case PilotClientGui: return gui; + case MappingTool: return mapping; + case UnitTest: return unitTest; + case Sample: return sample; + default: break; + } + return unknown; + } + bool CApplicationInfo::isSampleOrUnitTest() const { - const Application a = this->application(); + const Application a = this->getApplication(); return a == CApplicationInfo::Sample || a == CApplicationInfo::UnitTest; } bool CApplicationInfo::isUnitTest() const { - const Application a = this->application(); + const Application a = this->getApplication(); return a == CApplicationInfo::UnitTest; } bool CApplicationInfo::isNull() const { - return this->application() == Unknown && m_exePath.isNull(); + return this->getApplication() == Unknown && m_exePath.isNull(); + } + + QString CApplicationInfo::asOtherSwiftVersionString(const QString separator) const + { + return QStringLiteral("Version; ") % this->getVersionString() % QStringLiteral(" os: ") % this->getPlatform() % separator % + QStringLiteral("exe.path: ") % this->getExecutablePath() % separator % + QStringLiteral("app.data: ") % this->getApplicationDataDirectory(); } QString CApplicationInfo::convertToQString(bool i18n) const { - return QString("{ %1, %2, %3, %4 }").arg(QString::number(m_app), m_exePath, m_version, m_process.convertToQString(i18n)); + return QString("{ %1, %2, %3, %4 }").arg(this->getApplicationAsString(), m_exePath, m_version, m_process.convertToQString(i18n)); + } + + CIcon CApplicationInfo::toIcon() const + { + switch (getApplication()) + { + case Laucher: return CIconList::allIcons().findByIndex(CIcons::SwiftLauncher16); + case PilotClientCore: return CIconList::allIcons().findByIndex(CIcons::SwiftCore16); + case PilotClientGui: return CIconList::allIcons().findByIndex(CIcons::Swift16); + case MappingTool: return CIconList::allIcons().findByIndex(CIcons::SwiftDatabase16); + default: break; + } + return CIconList::allIcons().findByIndex(CIcons::StandardIconUnknown16); + } + + CVariant CApplicationInfo::propertyByIndex(const CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + const ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexApplication: return CVariant::fromValue(m_app); + case IndexApplicationAsString: return CVariant::fromValue(this->getApplicationAsString()); + case IndexApplicationDataPath: return CVariant::fromValue(this->getApplicationDataDirectory()); + case IndexCompileInfo: return CVariant::fromValue(this->getCompileInfo()); + case IndexExecutablePath: return CVariant::fromValue(this->getExecutablePath()); + case IndexPlatformInfo: return CVariant::fromValue(this->getPlatform()); + case IndexProcessInfo: return m_process.propertyByIndex(index.copyFrontRemoved()); + case IndexVersionString: return CVariant::fromValue(this->getVersionString()); + case IndexWordSize: return CVariant::fromValue(this->getWordSize()); + default: break; + } + return CValueObject::propertyByIndex(index); + } + + void CApplicationInfo::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + const ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexApplication: this->setApplication(static_cast(variant.toInt())); break; + case IndexApplicationAsString: break; + case IndexApplicationDataPath: this->setApplicationDataDirectory(variant.toQString()); break; + case IndexCompileInfo: this->setCompileInfo(variant.toQString()); break; + case IndexExecutablePath: this->setExecutablePath(variant.toQString()); break; + case IndexPlatformInfo: this->setPlatformInfo(variant.toQString()); break; + case IndexProcessInfo: m_process.setPropertyByIndex(index.copyFrontRemoved(), variant); break; + case IndexVersionString: this->setVersionString(variant.toQString()); break; + case IndexWordSize: return this->setWordSize(variant.toInt()); break; + default: CValueObject::setPropertyByIndex(index, variant); break; + } + } + + int CApplicationInfo::comparePropertyByIndex(const CPropertyIndex &index, const CApplicationInfo &compareValue) const + { + if (index.isMyself()) { return this->getExecutablePath().compare(compareValue.getExecutablePath()); } + const ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexApplicationDataPath: this->getApplicationDataDirectory().compare(compareValue.getApplicationDataDirectory()); + case IndexCompileInfo: this->getCompileInfo().compare(compareValue.getCompileInfo()); + case IndexExecutablePath: this->getExecutablePath().compare(compareValue.getExecutablePath()); + case IndexPlatformInfo: return this->getPlatform().compare(compareValue.getPlatform()); + case IndexProcessInfo: return this->getProcessInfo().processName().compare(compareValue.getProcessInfo().processName()); + case IndexVersionString: return this->getVersionString().compare(compareValue.getVersionString()); + case IndexWordSize: return Compare::compare(this->getWordSize(), compareValue.getWordSize()); + case IndexApplication: + case IndexApplicationAsString: + default: + return this->getApplicationAsString().compare(compareValue.getApplicationAsString()); + } } const QString &CApplicationInfo::swiftPilotClientGui() @@ -91,6 +196,12 @@ namespace BlackMisc return fn; } + const CApplicationInfo &CApplicationInfo::null() + { + static const CApplicationInfo n; + return n; + } + CApplicationInfo::Application CApplicationInfo::guessApplication() { const QString a(QCoreApplication::instance()->applicationName().toLower()); @@ -102,4 +213,4 @@ namespace BlackMisc if (a.contains("data") || a.contains("mapping")) { return CApplicationInfo::MappingTool; } return CApplicationInfo::Unknown; } -} +} // ns diff --git a/src/blackmisc/applicationinfo.h b/src/blackmisc/applicationinfo.h index e9f59861b..3cb7f9980 100644 --- a/src/blackmisc/applicationinfo.h +++ b/src/blackmisc/applicationinfo.h @@ -39,6 +39,20 @@ namespace BlackMisc Sample }; + //! Properties by index + enum ColumnIndex + { + IndexApplication = CPropertyIndex::GlobalIndexCApplicationInfo, + IndexApplicationAsString, + IndexApplicationDataPath, + IndexCompileInfo, + IndexExecutablePath, + IndexPlatformInfo, + IndexProcessInfo, + IndexVersionString, + IndexWordSize, + }; + //! Default constructor. CApplicationInfo(); @@ -49,7 +63,10 @@ namespace BlackMisc void setApplication(Application app) { m_app = static_cast(app); } //! Get application. - Application application() const { return static_cast(m_app); } + Application getApplication() const { return static_cast(m_app); } + + //! get application as string + const QString &getApplicationAsString() const; //! Set executable path. void setExecutablePath(const QString &exePath) { m_exePath = exePath; } @@ -57,6 +74,14 @@ namespace BlackMisc //! Get executable path. const QString &getExecutablePath() const { return m_exePath; } + //! Set application data dir + //! \remark rootdir of settings, cache and logs + void setApplicationDataDirectory(const QString &appDataDir) { m_applicationDataDir = appDataDir; } + + //! Set application data dir + //! \remark rootdir of settings, cache and logs + const QString &getApplicationDataDirectory() const { return m_applicationDataDir; } + //! Set version string. void setVersionString(const QString &version) { m_version = version; } @@ -96,9 +121,24 @@ namespace BlackMisc //! Null object bool isNull() const; + //! Formatted info + QString asOtherSwiftVersionString(const QString separator = " | ") const; + //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const; + //! \copydoc BlackMisc::Mixin::Icon::toIcon() + CIcon toIcon() const; + + //! \copydoc BlackMisc::Mixin::Index::propertyByIndex + CVariant propertyByIndex(const CPropertyIndex &index) const; + + //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex + void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant); + + //! Compare by index + int comparePropertyByIndex(const CPropertyIndex &index, const CApplicationInfo &compareValue) const; + //! Name of pilot client GUI static const QString &swiftPilotClientGui(); @@ -117,15 +157,20 @@ namespace BlackMisc //! File name of the application info file static const QString &fileName(); + //! NULL info + static const CApplicationInfo &null(); + private: int m_app = static_cast(Unknown); int m_wordSize; QString m_exePath; + QString m_applicationDataDir; QString m_version; QString m_compileInfo; QString m_platform; CProcessInfo m_process; + //! Guess Application static Application guessApplication(); BLACK_METACLASS( @@ -133,6 +178,7 @@ namespace BlackMisc BLACK_METAMEMBER(app), BLACK_METAMEMBER(wordSize), BLACK_METAMEMBER(exePath), + BLACK_METAMEMBER(applicationDataDir), BLACK_METAMEMBER(version), BLACK_METAMEMBER(compileInfo), BLACK_METAMEMBER(platform), diff --git a/src/blackmisc/applicationinfolist.cpp b/src/blackmisc/applicationinfolist.cpp index 09571109f..443a3678c 100644 --- a/src/blackmisc/applicationinfolist.cpp +++ b/src/blackmisc/applicationinfolist.cpp @@ -8,10 +8,14 @@ */ #include "blackmisc/applicationinfolist.h" +#include "directoryutils.h" +#include "blackconfig/buildconfig.h" + +using namespace BlackConfig; namespace BlackMisc { - CApplicationInfoList::CApplicationInfoList() = default; + CApplicationInfoList::CApplicationInfoList() {} CApplicationInfoList::CApplicationInfoList(const CSequence &other) : CSequence(other) @@ -19,12 +23,12 @@ namespace BlackMisc bool CApplicationInfoList::containsApplication(CApplicationInfo::Application application) const { - return this->contains(&CApplicationInfo::application, application); + return this->contains(&CApplicationInfo::getApplication, application); } int CApplicationInfoList::removeApplication(CApplicationInfo::Application application) { - return this->removeIf(&CApplicationInfo::application, application); + return this->removeIf(&CApplicationInfo::getApplication, application); } QStringList CApplicationInfoList::processNames() const @@ -37,4 +41,27 @@ namespace BlackMisc } return names; } -} + + int CApplicationInfoList::otherSwiftVersionsFromDataDirectories() + { + this->clear(); + const QMap otherVersions = CDirectoryUtils::applicationDataDirectoryMapWithoutCurrentVersion(); + for (const QString &directory : otherVersions.keys()) + { + CApplicationInfo info(otherVersions.value(directory)); + this->push_back(info); + } + return this->size(); + } + + CApplicationInfoList CApplicationInfoList::fromOtherSwiftVersionsFromDataDirectories() + { + static CApplicationInfoList info = [] + { + CApplicationInfoList il; + il.otherSwiftVersionsFromDataDirectories(); + return il; + }(); + return info; + } +} // ns diff --git a/src/blackmisc/applicationinfolist.h b/src/blackmisc/applicationinfolist.h index 6f5042250..ec87e7868 100644 --- a/src/blackmisc/applicationinfolist.h +++ b/src/blackmisc/applicationinfolist.h @@ -42,8 +42,14 @@ namespace BlackMisc //! Running application names QStringList processNames() const; + + //! Fill from cache data directories + int otherSwiftVersionsFromDataDirectories(); + + //! Filled from cache data directories + static CApplicationInfoList fromOtherSwiftVersionsFromDataDirectories(); }; -} +} // ns Q_DECLARE_METATYPE(BlackMisc::CApplicationInfoList) Q_DECLARE_METATYPE(BlackMisc::CSequence) diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index afd82d64f..ff9acdc98 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -87,6 +87,7 @@ namespace BlackMisc GlobalIndexCRgbColor = 800, GlobalIndexCCountry = 900, GlobalIndexCPlatform = 1000, + GlobalIndexCApplicationInfo = 1100, GlobalIndexCCallsign = 2000, GlobalIndexCAircraftSituation = 2100, GlobalIndexCAircraftSituationChange = 2200,