diff options
Diffstat (limited to 'api/logic/wonko/tasks')
4 files changed, 409 insertions, 0 deletions
diff --git a/api/logic/wonko/tasks/BaseWonkoEntityLocalLoadTask.cpp b/api/logic/wonko/tasks/BaseWonkoEntityLocalLoadTask.cpp new file mode 100644 index 00000000..b54c592f --- /dev/null +++ b/api/logic/wonko/tasks/BaseWonkoEntityLocalLoadTask.cpp @@ -0,0 +1,117 @@ +/* Copyright 2015 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 "BaseWonkoEntityLocalLoadTask.h" + +#include <QFile> + +#include "wonko/format/WonkoFormat.h" +#include "wonko/WonkoUtil.h" +#include "wonko/WonkoIndex.h" +#include "wonko/WonkoVersion.h" +#include "wonko/WonkoVersionList.h" +#include "Env.h" +#include "Json.h" + +BaseWonkoEntityLocalLoadTask::BaseWonkoEntityLocalLoadTask(BaseWonkoEntity *entity, QObject *parent) + : Task(parent), m_entity(entity) +{ +} + +void BaseWonkoEntityLocalLoadTask::executeTask() +{ + const QString fname = Wonko::localWonkoDir().absoluteFilePath(filename()); + if (!QFile::exists(fname)) + { + emitFailed(tr("File doesn't exist")); + return; + } + + setStatus(tr("Reading %1...").arg(name())); + setProgress(0, 0); + + try + { + parse(Json::requireObject(Json::requireDocument(fname, name()), name())); + m_entity->notifyLocalLoadComplete(); + emitSucceeded(); + } + catch (Exception &e) + { + emitFailed(tr("Unable to parse file %1: %2").arg(fname, e.cause())); + } +} + +// WONKO INDEX // +WonkoIndexLocalLoadTask::WonkoIndexLocalLoadTask(WonkoIndex *index, QObject *parent) + : BaseWonkoEntityLocalLoadTask(index, parent) +{ +} +QString WonkoIndexLocalLoadTask::filename() const +{ + return "index.json"; +} +QString WonkoIndexLocalLoadTask::name() const +{ + return tr("Wonko Index"); +} +void WonkoIndexLocalLoadTask::parse(const QJsonObject &obj) const +{ + WonkoFormat::parseIndex(obj, dynamic_cast<WonkoIndex *>(entity())); +} + +// WONKO VERSION LIST // +WonkoVersionListLocalLoadTask::WonkoVersionListLocalLoadTask(WonkoVersionList *list, QObject *parent) + : BaseWonkoEntityLocalLoadTask(list, parent) +{ +} +QString WonkoVersionListLocalLoadTask::filename() const +{ + return list()->uid() + ".json"; +} +QString WonkoVersionListLocalLoadTask::name() const +{ + return tr("Wonko Version List for %1").arg(list()->humanReadable()); +} +void WonkoVersionListLocalLoadTask::parse(const QJsonObject &obj) const +{ + WonkoFormat::parseVersionList(obj, list()); +} +WonkoVersionList *WonkoVersionListLocalLoadTask::list() const +{ + return dynamic_cast<WonkoVersionList *>(entity()); +} + +// WONKO VERSION // +WonkoVersionLocalLoadTask::WonkoVersionLocalLoadTask(WonkoVersion *version, QObject *parent) + : BaseWonkoEntityLocalLoadTask(version, parent) +{ +} +QString WonkoVersionLocalLoadTask::filename() const +{ + return version()->uid() + "/" + version()->version() + ".json"; +} +QString WonkoVersionLocalLoadTask::name() const +{ + return tr("Wonko Version for %1").arg(version()->name()); +} +void WonkoVersionLocalLoadTask::parse(const QJsonObject &obj) const +{ + WonkoFormat::parseVersion(obj, version()); +} +WonkoVersion *WonkoVersionLocalLoadTask::version() const +{ + return dynamic_cast<WonkoVersion *>(entity()); +} diff --git a/api/logic/wonko/tasks/BaseWonkoEntityLocalLoadTask.h b/api/logic/wonko/tasks/BaseWonkoEntityLocalLoadTask.h new file mode 100644 index 00000000..2affa17f --- /dev/null +++ b/api/logic/wonko/tasks/BaseWonkoEntityLocalLoadTask.h @@ -0,0 +1,81 @@ +/* Copyright 2015 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 "tasks/Task.h" +#include <memory> + +class BaseWonkoEntity; +class WonkoIndex; +class WonkoVersionList; +class WonkoVersion; + +class BaseWonkoEntityLocalLoadTask : public Task +{ + Q_OBJECT +public: + explicit BaseWonkoEntityLocalLoadTask(BaseWonkoEntity *entity, QObject *parent = nullptr); + +protected: + virtual QString filename() const = 0; + virtual QString name() const = 0; + virtual void parse(const QJsonObject &obj) const = 0; + + BaseWonkoEntity *entity() const { return m_entity; } + +private: + void executeTask() override; + + BaseWonkoEntity *m_entity; +}; + +class WonkoIndexLocalLoadTask : public BaseWonkoEntityLocalLoadTask +{ + Q_OBJECT +public: + explicit WonkoIndexLocalLoadTask(WonkoIndex *index, QObject *parent = nullptr); + +private: + QString filename() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; +}; +class WonkoVersionListLocalLoadTask : public BaseWonkoEntityLocalLoadTask +{ + Q_OBJECT +public: + explicit WonkoVersionListLocalLoadTask(WonkoVersionList *list, QObject *parent = nullptr); + +private: + QString filename() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + WonkoVersionList *list() const; +}; +class WonkoVersionLocalLoadTask : public BaseWonkoEntityLocalLoadTask +{ + Q_OBJECT +public: + explicit WonkoVersionLocalLoadTask(WonkoVersion *version, QObject *parent = nullptr); + +private: + QString filename() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + WonkoVersion *version() const; +}; diff --git a/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.cpp b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.cpp new file mode 100644 index 00000000..727ec89d --- /dev/null +++ b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.cpp @@ -0,0 +1,126 @@ +/* Copyright 2015 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 "BaseWonkoEntityRemoteLoadTask.h" + +#include "net/CacheDownload.h" +#include "net/HttpMetaCache.h" +#include "net/NetJob.h" +#include "wonko/format/WonkoFormat.h" +#include "wonko/WonkoUtil.h" +#include "wonko/WonkoIndex.h" +#include "wonko/WonkoVersion.h" +#include "wonko/WonkoVersionList.h" +#include "Env.h" +#include "Json.h" + +BaseWonkoEntityRemoteLoadTask::BaseWonkoEntityRemoteLoadTask(BaseWonkoEntity *entity, QObject *parent) + : Task(parent), m_entity(entity) +{ +} + +void BaseWonkoEntityRemoteLoadTask::executeTask() +{ + NetJob *job = new NetJob(name()); + + auto entry = ENV.metacache()->resolveEntry("wonko", url().toString()); + entry->setStale(true); + m_dl = CacheDownload::make(url(), entry); + job->addNetAction(m_dl); + connect(job, &NetJob::failed, this, &BaseWonkoEntityRemoteLoadTask::emitFailed); + connect(job, &NetJob::succeeded, this, &BaseWonkoEntityRemoteLoadTask::networkFinished); + connect(job, &NetJob::status, this, &BaseWonkoEntityRemoteLoadTask::setStatus); + connect(job, &NetJob::progress, this, &BaseWonkoEntityRemoteLoadTask::setProgress); + job->start(); +} + +void BaseWonkoEntityRemoteLoadTask::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())); + } +} + +// WONKO INDEX // +WonkoIndexRemoteLoadTask::WonkoIndexRemoteLoadTask(WonkoIndex *index, QObject *parent) + : BaseWonkoEntityRemoteLoadTask(index, parent) +{ +} +QUrl WonkoIndexRemoteLoadTask::url() const +{ + return Wonko::indexUrl(); +} +QString WonkoIndexRemoteLoadTask::name() const +{ + return tr("Wonko Index"); +} +void WonkoIndexRemoteLoadTask::parse(const QJsonObject &obj) const +{ + WonkoFormat::parseIndex(obj, dynamic_cast<WonkoIndex *>(entity())); +} + +// WONKO VERSION LIST // +WonkoVersionListRemoteLoadTask::WonkoVersionListRemoteLoadTask(WonkoVersionList *list, QObject *parent) + : BaseWonkoEntityRemoteLoadTask(list, parent) +{ +} +QUrl WonkoVersionListRemoteLoadTask::url() const +{ + return Wonko::versionListUrl(list()->uid()); +} +QString WonkoVersionListRemoteLoadTask::name() const +{ + return tr("Wonko Version List for %1").arg(list()->humanReadable()); +} +void WonkoVersionListRemoteLoadTask::parse(const QJsonObject &obj) const +{ + WonkoFormat::parseVersionList(obj, list()); +} +WonkoVersionList *WonkoVersionListRemoteLoadTask::list() const +{ + return dynamic_cast<WonkoVersionList *>(entity()); +} + +// WONKO VERSION // +WonkoVersionRemoteLoadTask::WonkoVersionRemoteLoadTask(WonkoVersion *version, QObject *parent) + : BaseWonkoEntityRemoteLoadTask(version, parent) +{ +} +QUrl WonkoVersionRemoteLoadTask::url() const +{ + return Wonko::versionUrl(version()->uid(), version()->version()); +} +QString WonkoVersionRemoteLoadTask::name() const +{ + return tr("Wonko Version for %1").arg(version()->name()); +} +void WonkoVersionRemoteLoadTask::parse(const QJsonObject &obj) const +{ + WonkoFormat::parseVersion(obj, version()); +} +WonkoVersion *WonkoVersionRemoteLoadTask::version() const +{ + return dynamic_cast<WonkoVersion *>(entity()); +} diff --git a/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.h b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.h new file mode 100644 index 00000000..91ed6af0 --- /dev/null +++ b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.h @@ -0,0 +1,85 @@ +/* Copyright 2015 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 "tasks/Task.h" +#include <memory> + +class BaseWonkoEntity; +class WonkoIndex; +class WonkoVersionList; +class WonkoVersion; + +class BaseWonkoEntityRemoteLoadTask : public Task +{ + Q_OBJECT +public: + explicit BaseWonkoEntityRemoteLoadTask(BaseWonkoEntity *entity, QObject *parent = nullptr); + +protected: + virtual QUrl url() const = 0; + virtual QString name() const = 0; + virtual void parse(const QJsonObject &obj) const = 0; + + BaseWonkoEntity *entity() const { return m_entity; } + +private slots: + void networkFinished(); + +private: + void executeTask() override; + + BaseWonkoEntity *m_entity; + std::shared_ptr<class CacheDownload> m_dl; +}; + +class WonkoIndexRemoteLoadTask : public BaseWonkoEntityRemoteLoadTask +{ + Q_OBJECT +public: + explicit WonkoIndexRemoteLoadTask(WonkoIndex *index, QObject *parent = nullptr); + +private: + QUrl url() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; +}; +class WonkoVersionListRemoteLoadTask : public BaseWonkoEntityRemoteLoadTask +{ + Q_OBJECT +public: + explicit WonkoVersionListRemoteLoadTask(WonkoVersionList *list, QObject *parent = nullptr); + +private: + QUrl url() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + WonkoVersionList *list() const; +}; +class WonkoVersionRemoteLoadTask : public BaseWonkoEntityRemoteLoadTask +{ + Q_OBJECT +public: + explicit WonkoVersionRemoteLoadTask(WonkoVersion *version, QObject *parent = nullptr); + +private: + QUrl url() const override; + QString name() const override; + void parse(const QJsonObject &obj) const override; + + WonkoVersion *version() const; +}; |