aboutsummaryrefslogtreecommitdiff
path: root/launcher/tasks
diff options
context:
space:
mode:
Diffstat (limited to 'launcher/tasks')
-rw-r--r--launcher/tasks/MultipleOptionsTask.cpp48
-rw-r--r--launcher/tasks/MultipleOptionsTask.h19
-rw-r--r--launcher/tasks/SequentialTask.cpp20
-rw-r--r--launcher/tasks/SequentialTask.h16
4 files changed, 90 insertions, 13 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();
+}
diff --git a/launcher/tasks/MultipleOptionsTask.h b/launcher/tasks/MultipleOptionsTask.h
new file mode 100644
index 00000000..7c508b00
--- /dev/null
+++ b/launcher/tasks/MultipleOptionsTask.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "SequentialTask.h"
+
+/* This task type will attempt to do run each of it's subtasks in sequence,
+ * until one of them succeeds. When that happens, the remaining tasks will not run.
+ * */
+class MultipleOptionsTask : public SequentialTask
+{
+ Q_OBJECT
+public:
+ explicit MultipleOptionsTask(QObject *parent = nullptr, const QString& task_name = "");
+ virtual ~MultipleOptionsTask() = default;
+
+private
+slots:
+ void startNext() override;
+ void subTaskFailed(const QString &msg) override;
+};
diff --git a/launcher/tasks/SequentialTask.cpp b/launcher/tasks/SequentialTask.cpp
index 7f03ad2e..f1e1a889 100644
--- a/launcher/tasks/SequentialTask.cpp
+++ b/launcher/tasks/SequentialTask.cpp
@@ -1,5 +1,7 @@
#include "SequentialTask.h"
+#include <QDebug>
+
SequentialTask::SequentialTask(QObject* parent, const QString& task_name) : Task(parent), m_name(task_name), m_currentIndex(-1) {}
SequentialTask::~SequentialTask()
@@ -39,14 +41,15 @@ bool SequentialTask::abort()
emit aborted();
emit finished();
}
- m_queue.clear();
+
+ m_aborted = true;
return true;
}
bool succeeded = m_queue[m_currentIndex]->abort();
- m_queue.clear();
+ m_aborted = succeeded;
- if(succeeded)
+ if (succeeded)
emitAborted();
return succeeded;
@@ -54,10 +57,14 @@ bool SequentialTask::abort()
void SequentialTask::startNext()
{
- if (m_currentIndex != -1) {
- Task::Ptr previous = m_queue[m_currentIndex];
+ if (m_aborted)
+ return;
+
+ if (m_currentIndex != -1 && m_currentIndex < m_queue.size()) {
+ Task::Ptr previous = m_queue.at(m_currentIndex);
disconnect(previous.get(), 0, this, 0);
}
+
m_currentIndex++;
if (m_queue.isEmpty() || m_currentIndex >= m_queue.size()) {
emitSucceeded();
@@ -76,6 +83,8 @@ void SequentialTask::startNext()
setStatus(tr("Executing task %1 out of %2").arg(m_currentIndex + 1).arg(m_queue.size()));
setStepStatus(next->isMultiStep() ? next->getStepStatus() : next->getStatus());
+ setProgress(m_currentIndex + 1, m_queue.count());
+
next->start();
}
@@ -93,7 +102,6 @@ void SequentialTask::subTaskProgress(qint64 current, qint64 total)
setProgress(0, 100);
return;
}
- setProgress(m_currentIndex + 1, m_queue.count());
m_stepProgress = current;
m_stepTotalProgress = total;
diff --git a/launcher/tasks/SequentialTask.h b/launcher/tasks/SequentialTask.h
index e10cb6f7..f5a58b1b 100644
--- a/launcher/tasks/SequentialTask.h
+++ b/launcher/tasks/SequentialTask.h
@@ -20,17 +20,17 @@ public:
void addTask(Task::Ptr task);
-protected slots:
- void executeTask() override;
public slots:
bool abort() override;
-private
+protected
slots:
- void startNext();
- void subTaskFailed(const QString &msg);
- void subTaskStatus(const QString &msg);
- void subTaskProgress(qint64 current, qint64 total);
+ void executeTask() override;
+
+ virtual void startNext();
+ virtual void subTaskFailed(const QString &msg);
+ virtual void subTaskStatus(const QString &msg);
+ virtual void subTaskProgress(qint64 current, qint64 total);
protected:
void setStepStatus(QString status) { m_step_status = status; emit stepStatus(status); };
@@ -44,4 +44,6 @@ protected:
qint64 m_stepProgress = 0;
qint64 m_stepTotalProgress = 100;
+
+ bool m_aborted = false;
};