summaryrefslogtreecommitdiffstats
path: root/api/logic/meta/tasks/RemoteLoadTask.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'api/logic/meta/tasks/RemoteLoadTask.cpp')
-rw-r--r--api/logic/meta/tasks/RemoteLoadTask.cpp139
1 files changed, 55 insertions, 84 deletions
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();
}
}