From 0060b506257b906d40ef53d1e23404dba76afcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Fri, 17 Mar 2017 01:48:54 +0100 Subject: [PATCH] NOISSUE simplify. --- api/logic/CMakeLists.txt | 2 - api/logic/Env.cpp | 2 +- api/logic/meta/BaseEntity.cpp | 7 +- api/logic/meta/BaseEntity.h | 5 +- api/logic/meta/Index.cpp | 27 ++-- api/logic/meta/Index.h | 7 +- api/logic/meta/Index_test.cpp | 6 +- api/logic/meta/Util.h | 3 - api/logic/meta/Version.cpp | 13 +- api/logic/meta/Version.h | 2 +- api/logic/meta/VersionList.cpp | 14 +- api/logic/meta/VersionList.h | 3 +- api/logic/meta/format/Format.cpp | 101 ++++++++++--- api/logic/meta/format/Format.h | 23 +-- api/logic/meta/format/FormatV1.cpp | 170 ---------------------- api/logic/meta/format/FormatV1.h | 33 ----- api/logic/meta/tasks/LocalLoadTask.cpp | 72 +-------- api/logic/meta/tasks/LocalLoadTask.h | 47 +----- api/logic/meta/tasks/RemoteLoadTask.cpp | 141 +++++++----------- api/logic/meta/tasks/RemoteLoadTask.h | 51 +------ application/pages/global/PackagesPage.cpp | 2 +- 21 files changed, 193 insertions(+), 538 deletions(-) delete mode 100644 api/logic/meta/format/FormatV1.cpp delete mode 100644 api/logic/meta/format/FormatV1.h diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt index b605fbd3..42754157 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -420,8 +420,6 @@ set(META_SOURCES meta/tasks/RemoteLoadTask.h meta/tasks/LocalLoadTask.cpp meta/tasks/LocalLoadTask.h - meta/format/FormatV1.cpp - meta/format/FormatV1.h meta/format/Format.cpp meta/format/Format.h meta/BaseEntity.cpp diff --git a/api/logic/Env.cpp b/api/logic/Env.cpp index 903f1d8a..d7be4a28 100644 --- a/api/logic/Env.cpp +++ b/api/logic/Env.cpp @@ -124,7 +124,7 @@ void Env::initHttpMetaCache() m_metacache->addBase("root", QDir::currentPath()); m_metacache->addBase("translations", QDir("translations").absolutePath()); m_metacache->addBase("icons", QDir("cache/icons").absolutePath()); - m_metacache->addBase("meta", QDir("cache/meta").absolutePath()); + m_metacache->addBase("meta", QDir("meta").absolutePath()); m_metacache->Load(); } diff --git a/api/logic/meta/BaseEntity.cpp b/api/logic/meta/BaseEntity.cpp index fd44e29c..87cd55c8 100644 --- a/api/logic/meta/BaseEntity.cpp +++ b/api/logic/meta/BaseEntity.cpp @@ -24,19 +24,18 @@ BaseEntity::~BaseEntity() { } -void BaseEntity::store() const +QUrl BaseEntity::url() const { - Json::write(serialized(), Meta::localDir().absoluteFilePath(localFilename())); + return rootUrl().resolved(localFilename()); } void BaseEntity::notifyLocalLoadComplete() { m_localLoaded = true; - store(); } + void BaseEntity::notifyRemoteLoadComplete() { m_remoteLoaded = true; - store(); } } diff --git a/api/logic/meta/BaseEntity.h b/api/logic/meta/BaseEntity.h index b7a241c6..7064e9d2 100644 --- a/api/logic/meta/BaseEntity.h +++ b/api/logic/meta/BaseEntity.h @@ -17,6 +17,7 @@ #include #include +#include #include "multimc_logic_export.h" @@ -33,10 +34,10 @@ public: virtual std::unique_ptr remoteUpdateTask() = 0; virtual std::unique_ptr localUpdateTask() = 0; virtual void merge(const std::shared_ptr &other) = 0; + virtual void parse(const QJsonObject &obj) = 0; - void store() const; virtual QString localFilename() const = 0; - virtual QJsonObject serialized() const = 0; + virtual QUrl url() const; bool isComplete() const { return m_localLoaded || m_remoteLoaded; } diff --git a/api/logic/meta/Index.cpp b/api/logic/meta/Index.cpp index 8a6b1355..e0e8bc5d 100644 --- a/api/logic/meta/Index.cpp +++ b/api/logic/meta/Index.cpp @@ -80,29 +80,36 @@ QVariant Index::headerData(int section, Qt::Orientation orientation, int role) c std::unique_ptr Index::remoteUpdateTask() { - return std::unique_ptr(new IndexRemoteLoadTask(this, this)); + return std::unique_ptr(new RemoteLoadTask(this)); } std::unique_ptr Index::localUpdateTask() { - return std::unique_ptr(new IndexLocalLoadTask(this, this)); -} - -QJsonObject Index::serialized() const -{ - return Format::serializeIndex(this); + return std::unique_ptr(new LocalLoadTask(this)); } bool Index::hasUid(const QString &uid) const { return m_uids.contains(uid); } -VersionListPtr Index::getList(const QString &uid) const + +VersionListPtr Index::get(const QString &uid) { return m_uids.value(uid, nullptr); } -VersionListPtr Index::getListGuaranteed(const QString &uid) const + +VersionPtr Index::get(const QString &uid, const QString &version) { - return m_uids.value(uid, std::make_shared(uid)); + auto list = get(uid); + if(list) + { + return list->getVersion(version); + } + return nullptr; +} + +void Index::parse(const QJsonObject& obj) +{ + parseIndex(obj, this); } void Index::merge(const Ptr &other) diff --git a/api/logic/meta/Index.h b/api/logic/meta/Index.h index 16e95d5a..37341656 100644 --- a/api/logic/meta/Index.h +++ b/api/logic/meta/Index.h @@ -27,6 +27,7 @@ class Task; namespace Meta { using VersionListPtr = std::shared_ptr; +using VersionPtr = std::shared_ptr; class MULTIMC_LOGIC_EXPORT Index : public QAbstractListModel, public BaseEntity { @@ -51,17 +52,21 @@ public: std::unique_ptr localUpdateTask() override; QString localFilename() const override { return "index.json"; } - QJsonObject serialized() const override; // queries + VersionListPtr get(const QString &uid); + VersionPtr get(const QString &uid, const QString &version); bool hasUid(const QString &uid) const; + /* VersionListPtr getList(const QString &uid) const; VersionListPtr getListGuaranteed(const QString &uid) const; + */ QVector lists() const { return m_lists; } public: // for usage by parsers only void merge(const BaseEntity::Ptr &other) override; + void parse(const QJsonObject &obj) override; private: QVector m_lists; diff --git a/api/logic/meta/Index_test.cpp b/api/logic/meta/Index_test.cpp index d26700ca..b4dbd009 100644 --- a/api/logic/meta/Index_test.cpp +++ b/api/logic/meta/Index_test.cpp @@ -27,9 +27,9 @@ slots: Meta::Index windex({std::make_shared("list1"), std::make_shared("list2"), std::make_shared("list3")}); QVERIFY(windex.hasUid("list1")); QVERIFY(!windex.hasUid("asdf")); - QVERIFY(windex.getList("list2") != nullptr); - QCOMPARE(windex.getList("list2")->uid(), QString("list2")); - QVERIFY(windex.getList("adsf") == nullptr); + QVERIFY(windex.get("list2") != nullptr); + QCOMPARE(windex.get("list2")->uid(), QString("list2")); + QVERIFY(windex.get("adsf") == nullptr); } void test_merge() diff --git a/api/logic/meta/Util.h b/api/logic/meta/Util.h index 28163fe4..188bcaa3 100644 --- a/api/logic/meta/Util.h +++ b/api/logic/meta/Util.h @@ -24,8 +24,5 @@ class QDir; namespace Meta { MULTIMC_LOGIC_EXPORT QUrl rootUrl(); -MULTIMC_LOGIC_EXPORT QUrl indexUrl(); -MULTIMC_LOGIC_EXPORT QUrl versionListUrl(const QString &uid); -MULTIMC_LOGIC_EXPORT QUrl versionUrl(const QString &uid, const QString &version); MULTIMC_LOGIC_EXPORT QDir localDir(); } diff --git a/api/logic/meta/Version.cpp b/api/logic/meta/Version.cpp index 039f4fef..b79c178a 100644 --- a/api/logic/meta/Version.cpp +++ b/api/logic/meta/Version.cpp @@ -48,11 +48,16 @@ QDateTime Version::time() const std::unique_ptr Version::remoteUpdateTask() { - return std::unique_ptr(new VersionRemoteLoadTask(this, this)); + return std::unique_ptr(new RemoteLoadTask(this)); } std::unique_ptr Version::localUpdateTask() { - return std::unique_ptr(new VersionLocalLoadTask(this, this)); + return std::unique_ptr(new LocalLoadTask(this)); +} + +void Version::parse(const QJsonObject& obj) +{ + parseVersion(obj, this); } void Version::merge(const std::shared_ptr &other) @@ -78,10 +83,6 @@ QString Version::localFilename() const { return m_uid + '/' + m_version + ".json"; } -QJsonObject Version::serialized() const -{ - return Format::serializeVersion(this); -} void Version::setType(const QString &type) { diff --git a/api/logic/meta/Version.h b/api/logic/meta/Version.h index 4a791880..635b79f6 100644 --- a/api/logic/meta/Version.h +++ b/api/logic/meta/Version.h @@ -59,9 +59,9 @@ public: std::unique_ptr remoteUpdateTask() override; std::unique_ptr localUpdateTask() override; void merge(const std::shared_ptr &other) override; + void parse(const QJsonObject &obj) override; QString localFilename() const override; - QJsonObject serialized() const override; public: // for usage by format parsers only void setType(const QString &type); diff --git a/api/logic/meta/VersionList.cpp b/api/logic/meta/VersionList.cpp index aab3e0f7..7196d4be 100644 --- a/api/logic/meta/VersionList.cpp +++ b/api/logic/meta/VersionList.cpp @@ -169,21 +169,17 @@ QHash VersionList::roleNames() const std::unique_ptr VersionList::remoteUpdateTask() { - return std::unique_ptr(new VersionListRemoteLoadTask(this, this)); + return std::unique_ptr(new RemoteLoadTask(this)); } std::unique_ptr VersionList::localUpdateTask() { - return std::unique_ptr(new VersionListLocalLoadTask(this, this)); + return std::unique_ptr(new LocalLoadTask(this)); } QString VersionList::localFilename() const { return m_uid + "/index.json"; } -QJsonObject VersionList::serialized() const -{ - return Format::serializeVersionList(this); -} QString VersionList::humanReadable() const { @@ -224,6 +220,11 @@ void VersionList::setVersions(const QVector &versions) endResetModel(); } +void VersionList::parse(const QJsonObject& obj) +{ + parseVersionList(obj, this); +} + void VersionList::merge(const BaseEntity::Ptr &other) { const VersionListPtr list = std::dynamic_pointer_cast(other); @@ -285,4 +286,5 @@ BaseVersionPtr VersionList::getRecommended() const } + #include "VersionList.moc" diff --git a/api/logic/meta/VersionList.h b/api/logic/meta/VersionList.h index 0eb85bf8..934b20e4 100644 --- a/api/logic/meta/VersionList.h +++ b/api/logic/meta/VersionList.h @@ -17,6 +17,7 @@ #include "BaseVersionList.h" #include "BaseEntity.h" +#include #include namespace Meta @@ -57,7 +58,6 @@ public: std::unique_ptr localUpdateTask() override; QString localFilename() const override; - QJsonObject serialized() const override; QString uid() const { return m_uid; } QString name() const { return m_name; } @@ -72,6 +72,7 @@ public: // for usage only by parsers void setName(const QString &name); void setVersions(const QVector &versions); void merge(const BaseEntity::Ptr &other) override; + void parse(const QJsonObject &obj) override; signals: void nameChanged(const QString &name); diff --git a/api/logic/meta/format/Format.cpp b/api/logic/meta/format/Format.cpp index e0fddb0b..39d3f14f 100644 --- a/api/logic/meta/format/Format.cpp +++ b/api/logic/meta/format/Format.cpp @@ -15,15 +15,85 @@ #include "Format.h" -#include "FormatV1.h" +#include "minecraft/onesix/OneSixVersionFormat.h"" #include "meta/Index.h" #include "meta/Version.h" #include "meta/VersionList.h" +#include "Json.h" +using namespace Json; + namespace Meta { +static const int currentFormatVersion = 0; + +// Index +static BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) +{ + const QVector objects = requireIsArrayOf(obj, "packages"); + QVector lists; + lists.reserve(objects.size()); + std::transform(objects.begin(), objects.end(), std::back_inserter(lists), [](const QJsonObject &obj) + { + VersionListPtr list = std::make_shared(requireString(obj, "uid")); + list->setName(ensureString(obj, "name", QString())); + return list; + }); + return std::make_shared(lists); +} + +// Version +static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) +{ + const QVector requiresRaw = obj.contains("requires") ? requireIsArrayOf(obj, "requires") : QVector(); + QVector requires; + requires.reserve(requiresRaw.size()); + std::transform(requiresRaw.begin(), requiresRaw.end(), std::back_inserter(requires), [](const QJsonObject &rObj) + { + Reference ref(requireString(rObj, "uid")); + ref.setVersion(ensureString(rObj, "version", QString())); + return ref; + }); + + VersionPtr version = std::make_shared(uid, requireString(obj, "version")); + version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); + version->setType(ensureString(obj, "type", QString())); + version->setRequires(requires); + return version; +} + +static BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) +{ + VersionPtr version = parseCommonVersion(requireString(obj, "uid"), obj); + + version->setData(OneSixVersionFormat::versionFileFromJson(QJsonDocument(obj), + QString("%1/%2.json").arg(version->uid(), version->version()), + obj.contains("order"))); + return version; +} + +// Version list / package +static BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) +{ + const QString uid = requireString(obj, "uid"); + + const QVector versionsRaw = requireIsArrayOf(obj, "versions"); + QVector versions; + versions.reserve(versionsRaw.size()); + std::transform(versionsRaw.begin(), versionsRaw.end(), std::back_inserter(versions), [uid](const QJsonObject &vObj) + { + return parseCommonVersion(uid, vObj); + }); + + VersionListPtr list = std::make_shared(uid); + list->setName(ensureString(obj, "name", QString())); + list->setVersions(versions); + return list; +} + + static int formatVersion(const QJsonObject &obj) { if (!obj.contains("formatVersion")) { @@ -35,56 +105,39 @@ static int formatVersion(const QJsonObject &obj) return obj.value("formatVersion").toInt(); } -void Format::parseIndex(const QJsonObject &obj, Index *ptr) +void parseIndex(const QJsonObject &obj, Index *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseIndexInternal(obj)); + ptr->merge(parseIndexInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersionList(const QJsonObject &obj, VersionList *ptr) +void parseVersionList(const QJsonObject &obj, VersionList *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseVersionListInternal(obj)); + ptr->merge(parseVersionListInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } -void Format::parseVersion(const QJsonObject &obj, Version *ptr) +void parseVersion(const QJsonObject &obj, Version *ptr) { const int version = formatVersion(obj); switch (version) { case 0: - ptr->merge(FormatV1().parseVersionInternal(obj)); + ptr->merge(parseVersionInternal(obj)); break; default: throw ParseException(QObject::tr("Unknown formatVersion: %1").arg(version)); } } - - -QJsonObject Format::serializeIndex(const Index *ptr) -{ - return FormatV1().serializeIndexInternal(ptr); -} - -QJsonObject Format::serializeVersionList(const VersionList *ptr) -{ - return FormatV1().serializeVersionListInternal(ptr); -} - -QJsonObject Format::serializeVersion(const Version *ptr) -{ - return FormatV1().serializeVersionInternal(ptr); -} - } diff --git a/api/logic/meta/format/Format.h b/api/logic/meta/format/Format.h index 79605a6f..aaed07fc 100644 --- a/api/logic/meta/format/Format.h +++ b/api/logic/meta/format/Format.h @@ -33,25 +33,8 @@ public: using Exception::Exception; }; -class Format -{ -public: - virtual ~Format() {} +void parseIndex(const QJsonObject &obj, Index *ptr); +void parseVersion(const QJsonObject &obj, Version *ptr); +void parseVersionList(const QJsonObject &obj, VersionList *ptr); - static void parseIndex(const QJsonObject &obj, Index *ptr); - static void parseVersion(const QJsonObject &obj, Version *ptr); - static void parseVersionList(const QJsonObject &obj, VersionList *ptr); - - static QJsonObject serializeIndex(const Index *ptr); - static QJsonObject serializeVersion(const Version *ptr); - static QJsonObject serializeVersionList(const VersionList *ptr); - -protected: - virtual BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) const = 0; - virtual BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) const = 0; - virtual BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) const = 0; - virtual QJsonObject serializeIndexInternal(const Index *ptr) const = 0; - virtual QJsonObject serializeVersionInternal(const Version *ptr) const = 0; - virtual QJsonObject serializeVersionListInternal(const VersionList *ptr) const = 0; -}; } diff --git a/api/logic/meta/format/FormatV1.cpp b/api/logic/meta/format/FormatV1.cpp deleted file mode 100644 index ee2c5291..00000000 --- a/api/logic/meta/format/FormatV1.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* Copyright 2015-2017 MultiMC Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "FormatV1.h" -#include - -#include "Json.h" - -#include "meta/Index.h" -#include "meta/Version.h" -#include "meta/VersionList.h" -#include "Env.h" - -using namespace Json; - -namespace Meta -{ - -static const int currentFormatVersion = 0; - -// Index - -BaseEntity::Ptr FormatV1::parseIndexInternal(const QJsonObject &obj) const -{ - const QVector objects = requireIsArrayOf(obj, "packages"); - QVector lists; - lists.reserve(objects.size()); - std::transform(objects.begin(), objects.end(), std::back_inserter(lists), [](const QJsonObject &obj) - { - VersionListPtr list = std::make_shared(requireString(obj, "uid")); - list->setName(ensureString(obj, "name", QString())); - return list; - }); - return std::make_shared(lists); -} - -QJsonObject FormatV1::serializeIndexInternal(const Index *ptr) const -{ - QJsonArray packages; - for (const VersionListPtr &list : ptr->lists()) - { - QJsonObject out; - out["uid"] = list->uid(); - out["name"] = list->name(); - packages.append(out); - } - QJsonObject out; - out["formatVersion"] = currentFormatVersion; - out["packages"] = packages; - return out; -} - - -// Version - -static VersionPtr parseCommonVersion(const QString &uid, const QJsonObject &obj) -{ - const QVector requiresRaw = obj.contains("requires") ? requireIsArrayOf(obj, "requires") : QVector(); - QVector requires; - requires.reserve(requiresRaw.size()); - std::transform(requiresRaw.begin(), requiresRaw.end(), std::back_inserter(requires), [](const QJsonObject &rObj) - { - Reference ref(requireString(rObj, "uid")); - ref.setVersion(ensureString(rObj, "version", QString())); - return ref; - }); - - VersionPtr version = std::make_shared(uid, requireString(obj, "version")); - version->setTime(QDateTime::fromString(requireString(obj, "releaseTime"), Qt::ISODate).toMSecsSinceEpoch() / 1000); - version->setType(ensureString(obj, "type", QString())); - version->setRequires(requires); - return version; -} - -BaseEntity::Ptr FormatV1::parseVersionInternal(const QJsonObject &obj) const -{ - VersionPtr version = parseCommonVersion(requireString(obj, "uid"), obj); - - version->setData(OneSixVersionFormat::versionFileFromJson(QJsonDocument(obj), - QString("%1/%2.json").arg(version->uid(), version->version()), - obj.contains("order"))); - return version; -} - -static void serializeCommonVersion(const Version *version, QJsonObject &obj) -{ - QJsonArray requires; - for (const Reference &ref : version->requires()) - { - if (ref.version().isEmpty()) - { - QJsonObject out; - out["uid"] = ref.uid(); - requires.append(out); - } - else - { - QJsonObject out; - out["uid"] = ref.uid(); - out["version"] = ref.version(); - requires.append(out); - } - } - - obj.insert("version", version->version()); - obj.insert("type", version->type()); - obj.insert("releaseTime", version->time().toString(Qt::ISODate)); - obj.insert("requires", requires); -} - -QJsonObject FormatV1::serializeVersionInternal(const Version *ptr) const -{ - QJsonObject obj = OneSixVersionFormat::versionFileToJson(ptr->data(), true).object(); - serializeCommonVersion(ptr, obj); - obj.insert("formatVersion", currentFormatVersion); - obj.insert("uid", ptr->uid()); - // TODO: the name should be looked up in the UI based on the uid - obj.insert("name", ENV.metadataIndex()->getListGuaranteed(ptr->uid())->name()); - - return obj; -} - - -// Version list / package - -BaseEntity::Ptr FormatV1::parseVersionListInternal(const QJsonObject &obj) const -{ - const QString uid = requireString(obj, "uid"); - - const QVector versionsRaw = requireIsArrayOf(obj, "versions"); - QVector versions; - versions.reserve(versionsRaw.size()); - std::transform(versionsRaw.begin(), versionsRaw.end(), std::back_inserter(versions), [this, uid](const QJsonObject &vObj) - { return parseCommonVersion(uid, vObj); }); - - VersionListPtr list = std::make_shared(uid); - list->setName(ensureString(obj, "name", QString())); - list->setVersions(versions); - return list; -} - -QJsonObject FormatV1::serializeVersionListInternal(const VersionList *ptr) const -{ - QJsonArray versions; - for (const VersionPtr &version : ptr->versions()) - { - QJsonObject obj; - serializeCommonVersion(version.get(), obj); - versions.append(obj); - } - QJsonObject out; - out["formatVersion"] = currentFormatVersion; - out["uid"] = ptr->uid(); - out["name"] = ptr->name().isNull() ? QJsonValue() : ptr->name(); - out["versions"] = versions; - return out; -} -} diff --git a/api/logic/meta/format/FormatV1.h b/api/logic/meta/format/FormatV1.h deleted file mode 100644 index ab986083..00000000 --- a/api/logic/meta/format/FormatV1.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright 2015-2017 MultiMC Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include "Format.h" - -namespace Meta -{ -class FormatV1 : public Format -{ -public: - BaseEntity::Ptr parseIndexInternal(const QJsonObject &obj) const override; - BaseEntity::Ptr parseVersionInternal(const QJsonObject &obj) const override; - BaseEntity::Ptr parseVersionListInternal(const QJsonObject &obj) const override; - - QJsonObject serializeIndexInternal(const Index *ptr) const override; - QJsonObject serializeVersionInternal(const Version *ptr) const override; - QJsonObject serializeVersionListInternal(const VersionList *ptr) const override; -}; -} diff --git a/api/logic/meta/tasks/LocalLoadTask.cpp b/api/logic/meta/tasks/LocalLoadTask.cpp index 33ea8016..b64fc5d3 100644 --- a/api/logic/meta/tasks/LocalLoadTask.cpp +++ b/api/logic/meta/tasks/LocalLoadTask.cpp @@ -34,19 +34,18 @@ LocalLoadTask::LocalLoadTask(BaseEntity *entity, QObject *parent) void LocalLoadTask::executeTask() { - const QString fname = Meta::localDir().absoluteFilePath(filename()); + const QString fname = Meta::localDir().absoluteFilePath(m_entity->localFilename()); if (!QFile::exists(fname)) { emitFailed(tr("File doesn't exist")); return; } - - setStatus(tr("Reading %1...").arg(name())); + setStatus(tr("Reading %1...").arg(fname)); setProgress(0, 0); try { - parse(Json::requireObject(Json::requireDocument(fname, name()), name())); + m_entity->parse(Json::requireObject(Json::requireDocument(fname, fname), fname)); m_entity->notifyLocalLoadComplete(); emitSucceeded(); } @@ -55,69 +54,4 @@ void LocalLoadTask::executeTask() emitFailed(tr("Unable to parse file %1: %2").arg(fname, e.cause())); } } - - -// INDEX -IndexLocalLoadTask::IndexLocalLoadTask(Index *index, QObject *parent) - : LocalLoadTask(index, parent) -{ -} -QString IndexLocalLoadTask::filename() const -{ - return "index.json"; -} -QString IndexLocalLoadTask::name() const -{ - return tr("Metadata Index"); -} -void IndexLocalLoadTask::parse(const QJsonObject &obj) const -{ - Format::parseIndex(obj, dynamic_cast(entity())); -} - - -// VERSION LIST -VersionListLocalLoadTask::VersionListLocalLoadTask(VersionList *list, QObject *parent) - : LocalLoadTask(list, parent) -{ -} -QString VersionListLocalLoadTask::filename() const -{ - return list()->uid() + ".json"; -} -QString VersionListLocalLoadTask::name() const -{ - return tr("Version List for %1").arg(list()->humanReadable()); -} -void VersionListLocalLoadTask::parse(const QJsonObject &obj) const -{ - Format::parseVersionList(obj, list()); -} -VersionList *VersionListLocalLoadTask::list() const -{ - return dynamic_cast(entity()); -} - - -// VERSION -VersionLocalLoadTask::VersionLocalLoadTask(Version *version, QObject *parent) - : LocalLoadTask(version, parent) -{ -} -QString VersionLocalLoadTask::filename() const -{ - return version()->uid() + "/" + version()->version() + ".json"; -} -QString VersionLocalLoadTask::name() const -{ - return tr(" Version for %1").arg(version()->name()); -} -void VersionLocalLoadTask::parse(const QJsonObject &obj) const -{ - Format::parseVersion(obj, version()); -} -Version *VersionLocalLoadTask::version() const -{ - return dynamic_cast(entity()); -} } diff --git a/api/logic/meta/tasks/LocalLoadTask.h b/api/logic/meta/tasks/LocalLoadTask.h index 36ae1b6f..905660ed 100644 --- a/api/logic/meta/tasks/LocalLoadTask.h +++ b/api/logic/meta/tasks/LocalLoadTask.h @@ -25,60 +25,15 @@ class Index; class VersionList; class Version; +// FIXME: this is now just an odd function, get rid of it class LocalLoadTask : public Task { Q_OBJECT public: explicit LocalLoadTask(BaseEntity *entity, QObject *parent = nullptr); -protected: - virtual QString filename() const = 0; - virtual QString name() const = 0; - virtual void parse(const QJsonObject &obj) const = 0; - - BaseEntity *entity() const { return m_entity; } - private: void executeTask() override; - BaseEntity *m_entity; }; - -class IndexLocalLoadTask : public LocalLoadTask -{ - Q_OBJECT -public: - explicit IndexLocalLoadTask(Index *index, QObject *parent = nullptr); - -private: - QString filename() const override; - QString name() const override; - void parse(const QJsonObject &obj) const override; -}; -class VersionListLocalLoadTask : public LocalLoadTask -{ - Q_OBJECT -public: - explicit VersionListLocalLoadTask(VersionList *list, QObject *parent = nullptr); - -private: - QString filename() const override; - QString name() const override; - void parse(const QJsonObject &obj) const override; - - VersionList *list() const; -}; -class VersionLocalLoadTask : public LocalLoadTask -{ - Q_OBJECT -public: - explicit VersionLocalLoadTask(Version *version, QObject *parent = nullptr); - -private: - QString filename() const override; - QString name() const override; - void parse(const QJsonObject &obj) const override; - - Version *version() const; -}; } diff --git a/api/logic/meta/tasks/RemoteLoadTask.cpp b/api/logic/meta/tasks/RemoteLoadTask.cpp index 69a59c07..b73af021 100644 --- a/api/logic/meta/tasks/RemoteLoadTask.cpp +++ b/api/logic/meta/tasks/RemoteLoadTask.cpp @@ -34,99 +34,70 @@ RemoteLoadTask::RemoteLoadTask(BaseEntity *entity, QObject *parent) { } +class ParsingValidator : public Net::Validator +{ +public: /* con/des */ + ParsingValidator(BaseEntity *entity) : m_entity(entity) + { + }; + virtual ~ParsingValidator() + { + }; + +public: /* methods */ + bool init(QNetworkRequest &) override + { + return true; + } + bool write(QByteArray & data) override + { + this->data.append(data); + return true; + } + bool abort() override + { + return true; + } + bool validate(QNetworkReply &) override + { + auto fname = m_entity->localFilename(); + try + { + m_entity->parse(Json::requireObject(Json::requireDocument(data, fname), fname)); + m_entity->notifyRemoteLoadComplete(); + return true; + } + catch (Exception &e) + { + qWarning() << "Unable to parse response:" << e.cause(); + return false; + } + } + +private: /* data */ + QByteArray data; + BaseEntity *m_entity; +}; + void RemoteLoadTask::executeTask() { - NetJob *job = new NetJob(name()); + // FIXME: leak here!!! + NetJob *job = new NetJob(tr("Download of meta file %1").arg(m_entity->localFilename())); - auto entry = ENV.metacache()->resolveEntry("meta", url().toString()); + auto url = m_entity->url(); + auto entry = ENV.metacache()->resolveEntry("meta", m_entity->localFilename()); entry->setStale(true); - m_dl = Net::Download::makeCached(url(), entry); + m_dl = Net::Download::makeCached(url, entry); + /* + * The validator parses the file and loads it into the object. + * If that fails, the file is not written to storage. + */ + m_dl->addValidator(new ParsingValidator(m_entity)); job->addNetAction(m_dl); connect(job, &NetJob::failed, this, &RemoteLoadTask::emitFailed); - connect(job, &NetJob::succeeded, this, &RemoteLoadTask::networkFinished); + connect(job, &NetJob::succeeded, this, &RemoteLoadTask::succeeded); connect(job, &NetJob::status, this, &RemoteLoadTask::setStatus); connect(job, &NetJob::progress, this, &RemoteLoadTask::setProgress); job->start(); } - -void RemoteLoadTask::networkFinished() -{ - setStatus(tr("Parsing...")); - setProgress(0, 0); - - try - { - parse(Json::requireObject(Json::requireDocument(m_dl->getTargetFilepath(), name()), name())); - m_entity->notifyRemoteLoadComplete(); - emitSucceeded(); - } - catch (Exception &e) - { - emitFailed(tr("Unable to parse response: %1").arg(e.cause())); - } -} - -// INDEX -IndexRemoteLoadTask::IndexRemoteLoadTask(Index *index, QObject *parent) - : RemoteLoadTask(index, parent) -{ -} -QUrl IndexRemoteLoadTask::url() const -{ - return Meta::indexUrl(); -} -QString IndexRemoteLoadTask::name() const -{ - return tr("Metadata Index"); -} -void IndexRemoteLoadTask::parse(const QJsonObject &obj) const -{ - Format::parseIndex(obj, dynamic_cast(entity())); -} - - -// VERSION LIST -VersionListRemoteLoadTask::VersionListRemoteLoadTask(VersionList *list, QObject *parent) - : RemoteLoadTask(list, parent) -{ -} -QUrl VersionListRemoteLoadTask::url() const -{ - return Meta::versionListUrl(list()->uid()); -} -QString VersionListRemoteLoadTask::name() const -{ - return tr("Version List for %1").arg(list()->humanReadable()); -} -void VersionListRemoteLoadTask::parse(const QJsonObject &obj) const -{ - Format::parseVersionList(obj, list()); -} -VersionList* Meta::VersionListRemoteLoadTask::list() const -{ - return dynamic_cast(entity()); -} - - -// VERSION -VersionRemoteLoadTask::VersionRemoteLoadTask(Version *version, QObject *parent) - : RemoteLoadTask(version, parent) -{ -} -QUrl VersionRemoteLoadTask::url() const -{ - return Meta::versionUrl(version()->uid(), version()->version()); -} -QString VersionRemoteLoadTask::name() const -{ - return tr("Meta Version for %1").arg(version()->name()); -} -void VersionRemoteLoadTask::parse(const QJsonObject &obj) const -{ - Format::parseVersion(obj, version()); -} -Version *VersionRemoteLoadTask::version() const -{ - return dynamic_cast(entity()); -} } diff --git a/api/logic/meta/tasks/RemoteLoadTask.h b/api/logic/meta/tasks/RemoteLoadTask.h index 3b09404f..6d81d8ea 100644 --- a/api/logic/meta/tasks/RemoteLoadTask.h +++ b/api/logic/meta/tasks/RemoteLoadTask.h @@ -30,66 +30,17 @@ class Index; class VersionList; class Version; +// FIXME: this is now just an oddly constructed NetJob, get rid of it. class RemoteLoadTask : public Task { Q_OBJECT public: explicit RemoteLoadTask(BaseEntity *entity, QObject *parent = nullptr); -protected: - virtual QUrl url() const = 0; - virtual QString name() const = 0; - virtual void parse(const QJsonObject &obj) const = 0; - - BaseEntity *entity() const { return m_entity; } - -private slots: - void networkFinished(); - private: void executeTask() override; BaseEntity *m_entity; std::shared_ptr m_dl; }; - -class IndexRemoteLoadTask : public RemoteLoadTask -{ - Q_OBJECT -public: - explicit IndexRemoteLoadTask(Index *index, QObject *parent = nullptr); - -private: - QUrl url() const override; - QString name() const override; - void parse(const QJsonObject &obj) const override; -}; - -class VersionListRemoteLoadTask : public RemoteLoadTask -{ - Q_OBJECT -public: - explicit VersionListRemoteLoadTask(VersionList *list, QObject *parent = nullptr); - -private: - QUrl url() const override; - QString name() const override; - void parse(const QJsonObject &obj) const override; - - VersionList *list() const; -}; - -class VersionRemoteLoadTask : public RemoteLoadTask -{ - Q_OBJECT -public: - explicit VersionRemoteLoadTask(Version *version, QObject *parent = nullptr); - -private: - QUrl url() const override; - QString name() const override; - void parse(const QJsonObject &obj) const override; - - Version *version() const; -}; } diff --git a/application/pages/global/PackagesPage.cpp b/application/pages/global/PackagesPage.cpp index 52efaf9a..81ad4da1 100644 --- a/application/pages/global/PackagesPage.cpp +++ b/application/pages/global/PackagesPage.cpp @@ -36,7 +36,7 @@ static QString formatRequires(const VersionPtr &version) QStringList lines; for (const Reference &ref : version->requires()) { - const QString readable = ENV.metadataIndex()->hasUid(ref.uid()) ? ENV.metadataIndex()->getList(ref.uid())->humanReadable() : ref.uid(); + const QString readable = ENV.metadataIndex()->hasUid(ref.uid()) ? ENV.metadataIndex()->get(ref.uid())->humanReadable() : ref.uid(); if (ref.version().isEmpty()) { lines.append(readable);