diff options
Diffstat (limited to 'api/logic/tasks')
-rw-r--r-- | api/logic/tasks/SequentialTask.cpp | 55 | ||||
-rw-r--r-- | api/logic/tasks/SequentialTask.h | 31 | ||||
-rw-r--r-- | api/logic/tasks/Task.cpp | 88 | ||||
-rw-r--r-- | api/logic/tasks/Task.h | 96 | ||||
-rw-r--r-- | api/logic/tasks/ThreadTask.cpp | 41 | ||||
-rw-r--r-- | api/logic/tasks/ThreadTask.h | 25 |
6 files changed, 336 insertions, 0 deletions
diff --git a/api/logic/tasks/SequentialTask.cpp b/api/logic/tasks/SequentialTask.cpp new file mode 100644 index 00000000..ac0e7820 --- /dev/null +++ b/api/logic/tasks/SequentialTask.cpp @@ -0,0 +1,55 @@ +#include "SequentialTask.h" + +SequentialTask::SequentialTask(QObject *parent) : Task(parent), m_currentIndex(-1) +{ +} + +void SequentialTask::addTask(std::shared_ptr<Task> task) +{ + m_queue.append(task); +} + +void SequentialTask::executeTask() +{ + m_currentIndex = -1; + startNext(); +} + +void SequentialTask::startNext() +{ + if (m_currentIndex != -1) + { + std::shared_ptr<Task> previous = m_queue[m_currentIndex]; + disconnect(previous.get(), 0, this, 0); + } + m_currentIndex++; + if (m_queue.isEmpty() || m_currentIndex >= m_queue.size()) + { + emitSucceeded(); + return; + } + std::shared_ptr<Task> next = m_queue[m_currentIndex]; + connect(next.get(), SIGNAL(failed(QString)), this, SLOT(subTaskFailed(QString))); + connect(next.get(), SIGNAL(status(QString)), this, SLOT(subTaskStatus(QString))); + connect(next.get(), SIGNAL(progress(qint64, qint64)), this, SLOT(subTaskProgress(qint64, qint64))); + connect(next.get(), SIGNAL(succeeded()), this, SLOT(startNext())); + next->start(); +} + +void SequentialTask::subTaskFailed(const QString &msg) +{ + emitFailed(msg); +} +void SequentialTask::subTaskStatus(const QString &msg) +{ + setStatus(msg); +} +void SequentialTask::subTaskProgress(qint64 current, qint64 total) +{ + if(total == 0) + { + setProgress(0, 100); + return; + } + setProgress(current, total); +} diff --git a/api/logic/tasks/SequentialTask.h b/api/logic/tasks/SequentialTask.h new file mode 100644 index 00000000..69031095 --- /dev/null +++ b/api/logic/tasks/SequentialTask.h @@ -0,0 +1,31 @@ +#pragma once + +#include "Task.h" + +#include <QQueue> +#include <memory> + +#include "multimc_logic_export.h" + +class MULTIMC_LOGIC_EXPORT SequentialTask : public Task +{ + Q_OBJECT +public: + explicit SequentialTask(QObject *parent = 0); + + void addTask(std::shared_ptr<Task> task); + +protected: + void executeTask(); + +private +slots: + void startNext(); + void subTaskFailed(const QString &msg); + void subTaskStatus(const QString &msg); + void subTaskProgress(qint64 current, qint64 total); + +private: + QQueue<std::shared_ptr<Task> > m_queue; + int m_currentIndex; +}; diff --git a/api/logic/tasks/Task.cpp b/api/logic/tasks/Task.cpp new file mode 100644 index 00000000..3c4e3188 --- /dev/null +++ b/api/logic/tasks/Task.cpp @@ -0,0 +1,88 @@ +/* Copyright 2013-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 "Task.h" + +#include <QDebug> + +Task::Task(QObject *parent) : QObject(parent) +{ +} + +void Task::setStatus(const QString &new_status) +{ + if(m_status != new_status) + { + m_status = new_status; + emit status(m_status); + } +} + +void Task::setProgress(qint64 current, qint64 total) +{ + m_progress = current; + m_progressTotal = total; + emit progress(m_progress, m_progressTotal); +} + +void Task::start() +{ + m_running = true; + emit started(); + executeTask(); +} + +void Task::emitFailed(QString reason) +{ + m_running = false; + m_finished = true; + m_succeeded = false; + m_failReason = reason; + qCritical() << "Task failed: " << reason; + emit failed(reason); + emit finished(); +} + +void Task::emitSucceeded() +{ + if (!m_running) { return; } // Don't succeed twice. + m_running = false; + m_finished = true; + m_succeeded = true; + qDebug() << "Task succeeded"; + emit succeeded(); + emit finished(); +} + +bool Task::isRunning() const +{ + return m_running; +} + +bool Task::isFinished() const +{ + return m_finished; +} + +bool Task::successful() const +{ + return m_succeeded; +} + +QString Task::failReason() const +{ + return m_failReason; +} + diff --git a/api/logic/tasks/Task.h b/api/logic/tasks/Task.h new file mode 100644 index 00000000..2b0ccbcd --- /dev/null +++ b/api/logic/tasks/Task.h @@ -0,0 +1,96 @@ +/* Copyright 2013-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 <QObject> +#include <QString> + +#include "multimc_logic_export.h" + +class MULTIMC_LOGIC_EXPORT Task : public QObject +{ + Q_OBJECT +public: + explicit Task(QObject *parent = 0); + virtual ~Task() {}; + + virtual bool isRunning() const; + + virtual bool isFinished() const; + + /*! + * True if this task was successful. + * If the task failed or is still running, returns false. + */ + virtual bool successful() const; + + /*! + * Returns the string that was passed to emitFailed as the error message when the task failed. + * If the task hasn't failed, returns an empty string. + */ + virtual QString failReason() const; + + virtual bool canAbort() const { return false; } + + QString getStatus() + { + return m_status; + } + + qint64 getProgress() + { + return m_progress; + } + + qint64 getTotalProgress() + { + return m_progressTotal; + } + +signals: + void started(); + void progress(qint64 current, qint64 total); + void finished(); + void succeeded(); + void failed(QString reason); + void status(QString status); + +public +slots: + virtual void start(); + virtual bool abort() { return false; }; + +protected: + virtual void executeTask() = 0; + +protected slots: + virtual void emitSucceeded(); + virtual void emitFailed(QString reason); + +public slots: + void setStatus(const QString &status); + void setProgress(qint64 current, qint64 total); + +protected: + bool m_running = false; + bool m_finished = false; + bool m_succeeded = false; + QString m_failReason = ""; + QString m_status; + int m_progress = 0; + int m_progressTotal = 100; +}; + diff --git a/api/logic/tasks/ThreadTask.cpp b/api/logic/tasks/ThreadTask.cpp new file mode 100644 index 00000000..ddd1dee5 --- /dev/null +++ b/api/logic/tasks/ThreadTask.cpp @@ -0,0 +1,41 @@ +#include "ThreadTask.h" +#include <QtConcurrentRun> +ThreadTask::ThreadTask(Task * internal, QObject *parent) : Task(parent), m_internal(internal) +{ +} + +void ThreadTask::start() +{ + connect(m_internal, SIGNAL(failed(QString)), SLOT(iternal_failed(QString))); + connect(m_internal, SIGNAL(progress(qint64,qint64)), SLOT(iternal_progress(qint64,qint64))); + connect(m_internal, SIGNAL(started()), SLOT(iternal_started())); + connect(m_internal, SIGNAL(status(QString)), SLOT(iternal_status(QString))); + connect(m_internal, SIGNAL(succeeded()), SLOT(iternal_succeeded())); + m_running = true; + QtConcurrent::run(m_internal, &Task::start); +} + +void ThreadTask::iternal_failed(QString reason) +{ + emitFailed(reason); +} + +void ThreadTask::iternal_progress(qint64 current, qint64 total) +{ + progress(current, total); +} + +void ThreadTask::iternal_started() +{ + emit started(); +} + +void ThreadTask::iternal_status(QString status) +{ + setStatus(status); +} + +void ThreadTask::iternal_succeeded() +{ + emitSucceeded(); +} diff --git a/api/logic/tasks/ThreadTask.h b/api/logic/tasks/ThreadTask.h new file mode 100644 index 00000000..718dbc91 --- /dev/null +++ b/api/logic/tasks/ThreadTask.h @@ -0,0 +1,25 @@ +#pragma once + +#include "Task.h" + +class ThreadTask : public Task +{ + Q_OBJECT +public: + explicit ThreadTask(Task * internal, QObject * parent = nullptr); + +protected: + void executeTask() {}; + +public slots: + virtual void start(); + +private slots: + void iternal_started(); + void iternal_progress(qint64 current, qint64 total); + void iternal_succeeded(); + void iternal_failed(QString reason); + void iternal_status(QString status); +private: + Task * m_internal; +};
\ No newline at end of file |