diff options
Diffstat (limited to 'logic/tasks')
-rw-r--r-- | logic/tasks/ProgressProvider.h | 42 | ||||
-rw-r--r-- | logic/tasks/SequentialTask.cpp | 77 | ||||
-rw-r--r-- | logic/tasks/SequentialTask.h | 32 | ||||
-rw-r--r-- | logic/tasks/Task.cpp | 84 | ||||
-rw-r--r-- | logic/tasks/Task.h | 66 | ||||
-rw-r--r-- | logic/tasks/ThreadTask.cpp | 41 | ||||
-rw-r--r-- | logic/tasks/ThreadTask.h | 25 |
7 files changed, 367 insertions, 0 deletions
diff --git a/logic/tasks/ProgressProvider.h b/logic/tasks/ProgressProvider.h new file mode 100644 index 00000000..15e453a3 --- /dev/null +++ b/logic/tasks/ProgressProvider.h @@ -0,0 +1,42 @@ +/* Copyright 2013 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> + +class ProgressProvider : public QObject +{ + Q_OBJECT +protected: + explicit ProgressProvider(QObject *parent = 0) : QObject(parent) + { + } +signals: + void started(); + void progress(qint64 current, qint64 total); + void succeeded(); + void failed(QString reason); + void status(QString status); + +public: + virtual QString getStatus() const = 0; + virtual void getProgress(qint64 ¤t, qint64 &total) = 0; + virtual bool isRunning() const = 0; +public +slots: + virtual void start() = 0; + virtual void abort() = 0; +}; diff --git a/logic/tasks/SequentialTask.cpp b/logic/tasks/SequentialTask.cpp new file mode 100644 index 00000000..63025eee --- /dev/null +++ b/logic/tasks/SequentialTask.cpp @@ -0,0 +1,77 @@ +#include "SequentialTask.h" + +SequentialTask::SequentialTask(QObject *parent) : + Task(parent), m_currentIndex(-1) +{ + +} + +QString SequentialTask::getStatus() const +{ + if (m_queue.isEmpty() || m_currentIndex >= m_queue.size()) + { + return QString(); + } + return m_queue.at(m_currentIndex)->getStatus(); +} + +void SequentialTask::getProgress(qint64 ¤t, qint64 &total) +{ + current = 0; + total = 0; + for (int i = 0; i < m_queue.size(); ++i) + { + qint64 subCurrent, subTotal; + m_queue.at(i)->getProgress(subCurrent, subTotal); + current += subCurrent; + total += subTotal; + } +} + +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())); + connect(next.get(), SIGNAL(succeeded()), this, SLOT(startNext())); + next->start(); + emit status(getStatus()); +} + +void SequentialTask::subTaskFailed(const QString &msg) +{ + emitFailed(msg); +} +void SequentialTask::subTaskStatus(const QString &msg) +{ + setStatus(msg); +} +void SequentialTask::subTaskProgress() +{ + qint64 current, total; + getProgress(current, total); + setProgress(100 * current / total); +} diff --git a/logic/tasks/SequentialTask.h b/logic/tasks/SequentialTask.h new file mode 100644 index 00000000..7f046928 --- /dev/null +++ b/logic/tasks/SequentialTask.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Task.h" + +#include <QQueue> +#include <memory> + +class SequentialTask : public Task +{ + Q_OBJECT +public: + explicit SequentialTask(QObject *parent = 0); + + virtual QString getStatus() const; + virtual void getProgress(qint64 ¤t, qint64 &total); + + 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(); + +private: + QQueue<std::shared_ptr<Task> > m_queue; + int m_currentIndex; +}; diff --git a/logic/tasks/Task.cpp b/logic/tasks/Task.cpp new file mode 100644 index 00000000..cb7a5443 --- /dev/null +++ b/logic/tasks/Task.cpp @@ -0,0 +1,84 @@ +/* Copyright 2013 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 "logger/QsLog.h" + +Task::Task(QObject *parent) : ProgressProvider(parent) +{ +} + +QString Task::getStatus() const +{ + return m_status; +} + +void Task::setStatus(const QString &new_status) +{ + m_status = new_status; + emit status(new_status); +} + +void Task::setProgress(int new_progress) +{ + m_progress = new_progress; + emit progress(new_progress, 100); +} + +void Task::getProgress(qint64 ¤t, qint64 &total) +{ + current = m_progress; + total = 100; +} + +void Task::start() +{ + m_running = true; + emit started(); + executeTask(); +} + +void Task::emitFailed(QString reason) +{ + m_running = false; + m_succeeded = false; + m_failReason = reason; + QLOG_ERROR() << "Task failed: " << reason; + emit failed(reason); +} + +void Task::emitSucceeded() +{ + m_running = false; + m_succeeded = true; + QLOG_INFO() << "Task succeeded"; + emit succeeded(); +} + +bool Task::isRunning() const +{ + return m_running; +} + +bool Task::successful() const +{ + return m_succeeded; +} + +QString Task::failReason() const +{ + return m_failReason; +} + diff --git a/logic/tasks/Task.h b/logic/tasks/Task.h new file mode 100644 index 00000000..80d5e38b --- /dev/null +++ b/logic/tasks/Task.h @@ -0,0 +1,66 @@ +/* Copyright 2013 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 "ProgressProvider.h" + +class Task : public ProgressProvider +{ + Q_OBJECT +public: + explicit Task(QObject *parent = 0); + + virtual QString getStatus() const; + virtual void getProgress(qint64 ¤t, qint64 &total); + virtual bool isRunning() 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; + +public +slots: + virtual void start(); + virtual void abort() {}; + +protected: + virtual void executeTask() = 0; + + virtual void emitSucceeded(); + virtual void emitFailed(QString reason); + +protected +slots: + void setStatus(const QString &status); + void setProgress(int progress); + +protected: + QString m_status; + int m_progress = 0; + bool m_running = false; + bool m_succeeded = false; + QString m_failReason = ""; +}; diff --git a/logic/tasks/ThreadTask.cpp b/logic/tasks/ThreadTask.cpp new file mode 100644 index 00000000..ddd1dee5 --- /dev/null +++ b/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/logic/tasks/ThreadTask.h b/logic/tasks/ThreadTask.h new file mode 100644 index 00000000..718dbc91 --- /dev/null +++ b/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 |