diff options
author | Petr Mrázek <peterix@gmail.com> | 2017-03-17 01:48:54 +0100 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2017-04-07 00:20:01 +0200 |
commit | 0060b506257b906d40ef53d1e23404dba76afcee (patch) | |
tree | 19060c5a8d6525bbed6a91afa249a17e678e37fa /api/logic/meta/tasks | |
parent | 40cf38bc3225c614e7e81d074f890e6f9da0507b (diff) | |
download | MultiMC-0060b506257b906d40ef53d1e23404dba76afcee.tar MultiMC-0060b506257b906d40ef53d1e23404dba76afcee.tar.gz MultiMC-0060b506257b906d40ef53d1e23404dba76afcee.tar.lz MultiMC-0060b506257b906d40ef53d1e23404dba76afcee.tar.xz MultiMC-0060b506257b906d40ef53d1e23404dba76afcee.zip |
NOISSUE simplify.
Diffstat (limited to 'api/logic/meta/tasks')
-rw-r--r-- | api/logic/meta/tasks/LocalLoadTask.cpp | 72 | ||||
-rw-r--r-- | api/logic/meta/tasks/LocalLoadTask.h | 47 | ||||
-rw-r--r-- | api/logic/meta/tasks/RemoteLoadTask.cpp | 139 | ||||
-rw-r--r-- | api/logic/meta/tasks/RemoteLoadTask.h | 51 |
4 files changed, 60 insertions, 249 deletions
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<Index *>(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<VersionList *>(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<Version *>(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) { } -void RemoteLoadTask::executeTask() -{ - NetJob *job = new NetJob(name()); - - auto entry = ENV.metacache()->resolveEntry("meta", url().toString()); - entry->setStale(true); - m_dl = Net::Download::makeCached(url(), entry); - job->addNetAction(m_dl); - connect(job, &NetJob::failed, this, &RemoteLoadTask::emitFailed); - connect(job, &NetJob::succeeded, this, &RemoteLoadTask::networkFinished); - connect(job, &NetJob::status, this, &RemoteLoadTask::setStatus); - connect(job, &NetJob::progress, this, &RemoteLoadTask::setProgress); - job->start(); -} - -void RemoteLoadTask::networkFinished() +class ParsingValidator : public Net::Validator { - setStatus(tr("Parsing...")); - setProgress(0, 0); +public: /* con/des */ + ParsingValidator(BaseEntity *entity) : m_entity(entity) + { + }; + virtual ~ParsingValidator() + { + }; - try +public: /* methods */ + bool init(QNetworkRequest &) override { - parse(Json::requireObject(Json::requireDocument(m_dl->getTargetFilepath(), name()), name())); - m_entity->notifyRemoteLoadComplete(); - emitSucceeded(); + return true; } - catch (Exception &e) + bool write(QByteArray & data) override { - emitFailed(tr("Unable to parse response: %1").arg(e.cause())); + 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; + } } -} - -// 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<Index *>(entity())); -} +private: /* data */ + QByteArray data; + BaseEntity *m_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 +void RemoteLoadTask::executeTask() { - return dynamic_cast<VersionList *>(entity()); -} + // FIXME: leak here!!! + NetJob *job = new NetJob(tr("Download of meta file %1").arg(m_entity->localFilename())); - -// 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<Version *>(entity()); + auto url = m_entity->url(); + auto entry = ENV.metacache()->resolveEntry("meta", m_entity->localFilename()); + entry->setStale(true); + 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::succeeded); + connect(job, &NetJob::status, this, &RemoteLoadTask::setStatus); + connect(job, &NetJob::progress, this, &RemoteLoadTask::setProgress); + job->start(); } } 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<Net::Download> 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; -}; } |