diff options
author | flow <flowlnlnln@gmail.com> | 2022-06-02 19:34:08 -0300 |
---|---|---|
committer | flow <flowlnlnln@gmail.com> | 2022-07-17 11:33:41 -0300 |
commit | 9a44c9221139428fa4e3bdf560f6bfdc6fcbe75d (patch) | |
tree | 08713b50e73a3facf164aca8ada358773a8dec1c /launcher/tasks/MultipleOptionsTask.cpp | |
parent | dd6aabf9ab2c974816aef4e889e059fa0cdad53b (diff) | |
download | PrismLauncher-9a44c9221139428fa4e3bdf560f6bfdc6fcbe75d.tar.gz PrismLauncher-9a44c9221139428fa4e3bdf560f6bfdc6fcbe75d.tar.bz2 PrismLauncher-9a44c9221139428fa4e3bdf560f6bfdc6fcbe75d.zip |
feat: add MultipleOptionsTask
This is a variation of a Sequential Task, in which a subtask failing
will prompt the next one to execute, and a subtask being successful will
stop the task.
This way, this can be used for easily managing fallbacks with tasks. :D
Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/tasks/MultipleOptionsTask.cpp')
-rw-r--r-- | launcher/tasks/MultipleOptionsTask.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/launcher/tasks/MultipleOptionsTask.cpp b/launcher/tasks/MultipleOptionsTask.cpp new file mode 100644 index 00000000..6e853568 --- /dev/null +++ b/launcher/tasks/MultipleOptionsTask.cpp @@ -0,0 +1,48 @@ +#include "MultipleOptionsTask.h" + +#include <QDebug> + +MultipleOptionsTask::MultipleOptionsTask(QObject* parent, const QString& task_name) : SequentialTask(parent, task_name) {} + +void MultipleOptionsTask::startNext() +{ + Task* previous = nullptr; + if (m_currentIndex != -1) { + previous = m_queue[m_currentIndex].get(); + disconnect(previous, 0, this, 0); + } + + m_currentIndex++; + if ((previous && previous->wasSuccessful())) { + emitSucceeded(); + return; + } + + Task::Ptr next = m_queue[m_currentIndex]; + + connect(next.get(), &Task::failed, this, &MultipleOptionsTask::subTaskFailed); + connect(next.get(), &Task::succeeded, this, &MultipleOptionsTask::startNext); + + connect(next.get(), &Task::status, this, &MultipleOptionsTask::subTaskStatus); + connect(next.get(), &Task::stepStatus, this, &MultipleOptionsTask::subTaskStatus); + + connect(next.get(), &Task::progress, this, &MultipleOptionsTask::subTaskProgress); + + qDebug() << QString("Making attemp %1 out of %2").arg(m_currentIndex + 1).arg(m_queue.size()); + setStatus(tr("Making attempt #%1 out of %2").arg(m_currentIndex + 1).arg(m_queue.size())); + setStepStatus(next->isMultiStep() ? next->getStepStatus() : next->getStatus()); + + next->start(); +} + +void MultipleOptionsTask::subTaskFailed(QString const& reason) +{ + qDebug() << QString("Failed attempt #%1 of %2. Reason: %3").arg(m_currentIndex + 1).arg(m_queue.size()).arg(reason); + if(m_currentIndex < m_queue.size() - 1) { + startNext(); + return; + } + + qWarning() << QString("All attempts have failed!"); + emitFailed(); +} |