NOISSUE model Task states as one enum instead of multiple flags

This adds Task::State::AbortedByUser as a possibility
This commit is contained in:
Petr Mrázek 2018-12-10 20:50:15 +01:00
parent fb29e45bd0
commit de568b32b8
3 changed files with 51 additions and 20 deletions

View File

@ -58,6 +58,7 @@ void FtbPackInstallTask::onDownloadSucceeded()
void FtbPackInstallTask::onDownloadFailed(QString reason) void FtbPackInstallTask::onDownloadFailed(QString reason)
{ {
abortable = false;
emitFailed(reason); emitFailed(reason);
} }

View File

@ -39,23 +39,49 @@ void Task::setProgress(qint64 current, qint64 total)
void Task::start() void Task::start()
{ {
m_running = true; switch(m_state)
{
case State::Inactive:
{
qDebug() << "Task" << describe() << "starting for the first time";
break;
}
case State::AbortedByUser:
{
qDebug() << "Task" << describe() << "restarting for after being aborted by user";
break;
}
case State::Failed:
{
qDebug() << "Task" << describe() << "restarting for after failing at first";
break;
}
case State::Succeeded:
{
qDebug() << "Task" << describe() << "restarting for after succeeding at first";
break;
}
case State::Running:
{
qWarning() << "MultiMC tried to start task" << describe() << "while it was already running!";
return;
}
}
// NOTE: only fall thorugh to here in end states
m_state = State::Running;
emit started(); emit started();
qDebug() << "Task" << describe() << "started";
executeTask(); executeTask();
} }
void Task::emitFailed(QString reason) void Task::emitFailed(QString reason)
{ {
// Don't fail twice. // Don't fail twice.
if (!m_running) if (!isRunning())
{ {
qCritical() << "Task" << describe() << "failed while not running!!!!: " << reason; qCritical() << "Task" << describe() << "failed while not running!!!!: " << reason;
return; return;
} }
m_running = false; m_state = State::Failed;
m_finished = true;
m_succeeded = false;
m_failReason = reason; m_failReason = reason;
qCritical() << "Task" << describe() << "failed: " << reason; qCritical() << "Task" << describe() << "failed: " << reason;
emit failed(reason); emit failed(reason);
@ -65,14 +91,12 @@ void Task::emitFailed(QString reason)
void Task::emitAborted() void Task::emitAborted()
{ {
// Don't abort twice. // Don't abort twice.
if (!m_running) if (!isRunning())
{ {
qCritical() << "Task" << describe() << "aborted while not running!!!!"; qCritical() << "Task" << describe() << "aborted while not running!!!!";
return; return;
} }
m_running = false; m_state = State::AbortedByUser;
m_finished = true;
m_succeeded = false;
m_failReason = "Aborted."; m_failReason = "Aborted.";
qDebug() << "Task" << describe() << "aborted."; qDebug() << "Task" << describe() << "aborted.";
emit failed(m_failReason); emit failed(m_failReason);
@ -82,14 +106,12 @@ void Task::emitAborted()
void Task::emitSucceeded() void Task::emitSucceeded()
{ {
// Don't succeed twice. // Don't succeed twice.
if (!m_running) if (!isRunning())
{ {
qCritical() << "Task" << describe() << "succeeded while not running!!!!"; qCritical() << "Task" << describe() << "succeeded while not running!!!!";
return; return;
} }
m_running = false; m_state = State::Succeeded;
m_finished = true;
m_succeeded = true;
qDebug() << "Task" << describe() << "succeeded"; qDebug() << "Task" << describe() << "succeeded";
emit succeeded(); emit succeeded();
emit finished(); emit finished();
@ -116,17 +138,17 @@ QString Task::describe()
bool Task::isRunning() const bool Task::isRunning() const
{ {
return m_running; return m_state == State::Running;
} }
bool Task::isFinished() const bool Task::isFinished() const
{ {
return m_finished; return m_state != State::Running && m_state != State::Inactive;
} }
bool Task::wasSuccessful() const bool Task::wasSuccessful() const
{ {
return m_succeeded; return m_state == State::Succeeded;
} }
QString Task::failReason() const QString Task::failReason() const

View File

@ -24,6 +24,16 @@
class MULTIMC_LOGIC_EXPORT Task : public QObject class MULTIMC_LOGIC_EXPORT Task : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
enum class State
{
Inactive,
Running,
Succeeded,
Failed,
AbortedByUser
};
public: public:
explicit Task(QObject *parent = 0); explicit Task(QObject *parent = 0);
virtual ~Task() {}; virtual ~Task() {};
@ -88,9 +98,7 @@ public slots:
void setProgress(qint64 current, qint64 total); void setProgress(qint64 current, qint64 total);
private: private:
bool m_running = false; State m_state = State::Inactive;
bool m_finished = false;
bool m_succeeded = false;
QStringList m_Warnings; QStringList m_Warnings;
QString m_failReason = ""; QString m_failReason = "";
QString m_status; QString m_status;