From f448d50e29303fb06af7af2295c8d24332c36387 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 13 Nov 2015 00:47:28 +0100 Subject: [PATCH] refs #507 download value object for download information --- src/blackcore/blackcorefreefunctions.cpp | 3 + src/blackcore/data/download.cpp | 140 +++++++++++++++++++++++ src/blackcore/data/download.h | 133 +++++++++++++++++++++ src/blackmisc/propertyindex.h | 1 + 4 files changed, 277 insertions(+) create mode 100644 src/blackcore/data/download.cpp create mode 100644 src/blackcore/data/download.h diff --git a/src/blackcore/blackcorefreefunctions.cpp b/src/blackcore/blackcorefreefunctions.cpp index e8b913238..e71ab70e0 100644 --- a/src/blackcore/blackcorefreefunctions.cpp +++ b/src/blackcore/blackcorefreefunctions.cpp @@ -6,6 +6,8 @@ #include "blackcorefreefunctions.h" #include "blackcore/webreaderflags.h" #include "blackcore/data/globalsetup.h" +#include "blackcore/data/download.h" + #include "voice_channel.h" #include "network.h" @@ -26,5 +28,6 @@ namespace BlackCore qDBusRegisterMetaType(); BlackCore::Data::CGlobalSetup::registerMetadata(); + BlackCore::Data::CDownload::registerMetadata(); } } // namespace diff --git a/src/blackcore/data/download.cpp b/src/blackcore/data/download.cpp new file mode 100644 index 000000000..118ad2b18 --- /dev/null +++ b/src/blackcore/data/download.cpp @@ -0,0 +1,140 @@ +/* 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 "download.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include "blackmisc/project.h" +#include + +using namespace BlackMisc; +using namespace BlackMisc::Network; + +namespace BlackCore +{ + namespace Data + { + CDownload::CDownload() : + ITimestampBased(0), + m_downloadsStableUrls(QStringList {"http://swift-project.org/"}), + m_downloadsBetaUrls(QStringList {"http://swift-project.org/"}), + m_latestVersionStable(CProject::version()), + m_lastSupportedVersionStable("0.6"), + m_latestVersionBeta(CProject::version()), + m_lastSupportedVersionBeta("0.6") + { } + + CUrlList CDownload::getDownloadUrls() const + { + return CProject::isBetaTest() ? getDownloadUrlsBeta() : getDownloadUrlsStable(); + } + + QString CDownload::getLatestVersion() const + { + return CProject::isBetaTest() ? getLatestVersionBeta() : getLatestVersionStable(); + } + + bool CDownload::hasSameType(const CDownload &otherDownload) const + { + return this->isDevelopment() == otherDownload.isDevelopment(); + } + + QString CDownload::convertToQString(bool i18n) const + { + return convertToQString(", ", i18n); + } + + QString CDownload::convertToQString(const QString &separator, bool i18n) const + { + QString s("timestamp: "); + s.append(this->getFormattedUtcTimestampYmdhms()); + s.append(separator); + s.append("Download URLs (stable): "); + s.append(getDownloadUrlsStable().toQString(i18n)); + s.append(separator); + s.append("Download URLs (beta): "); + s.append(getDownloadUrlsBeta().toQString(i18n)); + s.append(separator); + s.append("Latest version (stable): "); + s.append(getLatestVersionStable()); + s.append(separator); + s.append("Latest version (beta): "); + s.append(getLatestVersionBeta()); + s.append(separator); + s.append("Latest supported version (stable): "); + s.append(getLastSupportedVersionStable()); + s.append(separator); + s.append("Latest supported version (beta): "); + s.append(getLastSupportedVersionBeta()); + s.append(separator); + return s; + } + + CVariant CDownload::propertyByIndex(const BlackMisc::CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::propertyByIndex(index); } + + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexDownloadUrlsStable: + return CVariant::fromValue(this->m_downloadsStableUrls); + case IndexDownloadUrlsBeta: + return CVariant::fromValue(this->m_downloadsBetaUrls); + case IndexLastSupportedVersionStable: + return CVariant::fromValue(this->m_lastSupportedVersionStable); + case IndexLatestVersionStable: + return CVariant::fromValue(this->m_latestVersionStable); + case IndexLastSupportedVersionBeta: + return CVariant::fromValue(this->m_lastSupportedVersionBeta); + case IndexLatestVersionBeta: + return CVariant::fromValue(this->m_latestVersionBeta); + default: + return CValueObject::propertyByIndex(index); + } + } + + void CDownload::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + if (ITimestampBased::canHandleIndex(index)) + { + ITimestampBased::setPropertyByIndex(variant, index); + return; + } + + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexDownloadUrlsStable: + this->m_downloadsStableUrls = variant.value(); + break; + case IndexDownloadUrlsBeta: + this->m_downloadsBetaUrls = variant.value(); + break; + case IndexLastSupportedVersionStable: + this->m_lastSupportedVersionStable = variant.toQString(); + break; + case IndexLatestVersionStable: + this->m_latestVersionStable = variant.toQString(); + break; + case IndexLastSupportedVersionBeta: + this->m_lastSupportedVersionBeta = variant.toQString(); + break; + case IndexLatestVersionBeta: + this->m_latestVersionBeta = variant.toQString(); + break; + default: + CValueObject::setPropertyByIndex(variant, index); + break; + } + } + + } // ns +} // ns diff --git a/src/blackcore/data/download.h b/src/blackcore/data/download.h new file mode 100644 index 000000000..eef1e479d --- /dev/null +++ b/src/blackcore/data/download.h @@ -0,0 +1,133 @@ +/* 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 BLACKCORE_DATA_DOWNLOAD_H +#define BLACKCORE_DATA_DOWNLOAD_H + +#include "blackcore/blackcoreexport.h" +#include "blackcore/datacache.h" +#include "blackmisc/network/urllist.h" +#include "blackmisc/valueobject.h" +#include "blackmisc/variant.h" + +namespace BlackCore +{ + namespace Data + { + //! Download locations and versions + class BLACKCORE_EXPORT CDownload : + public BlackMisc::CValueObject, + public BlackMisc::ITimestampBased + { + public: + //! Properties by index + enum ColumnIndex + { + IndexDownloadUrlsStable = BlackMisc::CPropertyIndex::GlobalIndexCDownload, + IndexDownloadUrlsBeta, + IndexLatestVersionStable, + IndexLastSupportedVersionStable, + IndexLatestVersionBeta, + IndexLastSupportedVersionBeta + }; + + //! Default constructor + CDownload(); + + //! Destructor. + ~CDownload() {} + + //! Download URLs, ie here one can download installer + const BlackMisc::Network::CUrlList &getDownloadUrlsBeta() const { return m_downloadsBetaUrls; } + + //! Download URLs, ie here one can download installer + const BlackMisc::Network::CUrlList &getDownloadUrlsStable() const { return m_downloadsStableUrls; } + + //! Download URLs + BlackMisc::Network::CUrlList getDownloadUrls() const; + + //! Latest version stable channel + const QString &getLatestVersionStable() const { return m_latestVersionStable; } + + //! Latest version beta channel + const QString &getLatestVersionBeta() const { return m_latestVersionBeta; } + + //! Latest version + QString getLatestVersion() const; + + //! Last supported version stable + const QString &getLastSupportedVersionStable() const { return m_lastSupportedVersionStable; } + + //! Last supported version beta + const QString &getLastSupportedVersionBeta() const { return m_lastSupportedVersionStable; } + + //! Productive settings? + bool isDevelopment() const { return m_development; } + + //! Same type + bool hasSameType(const CDownload &otherDownload) const; + + //! Productive settings? + void setDevelopment(bool development) { m_development = development; } + + //! \copydoc CValueObject::convertToQString + QString convertToQString(bool i18n = false) const; + + //! To string + QString convertToQString(const QString &separator, bool i18n = false) const; + + //! \copydoc CValueObject::propertyByIndex + BlackMisc::CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + + //! \copydoc CValueObject::setPropertyByIndex + void setPropertyByIndex(const BlackMisc::CVariant &variant, const BlackMisc::CPropertyIndex &index); + + private: + BLACK_ENABLE_TUPLE_CONVERSION(BlackCore::Data::CDownload) + + bool m_development = false; //!< for development + BlackMisc::Network::CUrlList m_downloadsStableUrls; //!< Download URLs, here I get the installer + BlackMisc::Network::CUrlList m_downloadsBetaUrls; //!< Download URLs, here I get the installer + QString m_latestVersionStable; //!< latest version + QString m_lastSupportedVersionStable; //!< last supported version + QString m_latestVersionBeta; //!< latest version + QString m_lastSupportedVersionBeta; //!< last supported version + }; + + //! Trait for global setup data + struct Download : public BlackCore::CDataTrait + { + //! Key in data cache + static const char *key() { return "readers/global/download"; } + + //! Default value + static const CDownload &defaultValue() + { + static const CDownload defaultValue; + return defaultValue; + } + }; + + } // ns +} // ns + +Q_DECLARE_METATYPE(BlackCore::Data::CDownload) +BLACK_DECLARE_TUPLE_CONVERSION(BlackCore::Data::CDownload, ( + attr(o.m_timestampMSecsSinceEpoch), + attr(o.m_development), + attr(o.m_downloadsStableUrls), + attr(o.m_downloadsBetaUrls), + attr(o.m_latestVersionStable), + attr(o.m_lastSupportedVersionStable), + attr(o.m_latestVersionBeta), + attr(o.m_lastSupportedVersionBeta) + )) +#endif // guard diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index c6ad6eb04..0b1a989eb 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -89,6 +89,7 @@ namespace BlackMisc GlobalIndexIDatastoreInteger = 11000, GlobalIndexIDatastoreString = 11100, GlobalIndexCGlobalSetup = 12000, + GlobalIndexCDownload = 12100, GlobalIndexAbuseMode = 20000 // property index abused as map key or otherwise, to be removed if no longer needed };