fix: abort and fail logic in tasks

Also sets up correctly the status connections
This commit is contained in:
flow 2022-05-01 11:05:31 -03:00
parent 040ee919e5
commit 57d65177c8
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
4 changed files with 11 additions and 4 deletions

View File

@ -69,6 +69,8 @@ void Download::addValidator(Validator* v)
void Download::executeTask() void Download::executeTask()
{ {
setStatus(tr("Downloading %1").arg(m_url.toString()));
if (getState() == Task::State::AbortedByUser) { if (getState() == Task::State::AbortedByUser) {
qWarning() << "Attempt to start an aborted Download:" << m_url.toString(); qWarning() << "Attempt to start an aborted Download:" << m_url.toString();
emitAborted(); emitAborted();
@ -90,6 +92,7 @@ void Download::executeTask()
emitFailed(); emitFailed();
return; return;
case State::AbortedByUser: case State::AbortedByUser:
emitAborted();
return; return;
} }
@ -216,13 +219,13 @@ void Download::downloadFinished()
qDebug() << "Download failed in previous step:" << m_url.toString(); qDebug() << "Download failed in previous step:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emitFailed(); emit failed("");
return; return;
} else if (m_state == State::AbortedByUser) { } else if (m_state == State::AbortedByUser) {
qDebug() << "Download aborted in previous step:" << m_url.toString(); qDebug() << "Download aborted in previous step:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emitAborted(); emit aborted();
return; return;
} }
@ -239,7 +242,7 @@ void Download::downloadFinished()
qDebug() << "Download failed to finalize:" << m_url.toString(); qDebug() << "Download failed to finalize:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emitFailed(); emit failed("");
return; return;
} }

View File

@ -47,7 +47,9 @@ auto NetJob::addNetAction(NetAction::Ptr action) -> bool
if (action->isRunning()) { if (action->isRunning()) {
connect(action.get(), &NetAction::succeeded, [this, action]{ partSucceeded(action->index()); }); connect(action.get(), &NetAction::succeeded, [this, action]{ partSucceeded(action->index()); });
connect(action.get(), &NetAction::failed, [this, action](QString){ partFailed(action->index()); }); connect(action.get(), &NetAction::failed, [this, action](QString){ partFailed(action->index()); });
connect(action.get(), &NetAction::aborted, [this, action](){ partAborted(action->index()); });
connect(action.get(), &NetAction::progress, [this, action](qint64 done, qint64 total) { partProgress(action->index(), done, total); }); connect(action.get(), &NetAction::progress, [this, action](qint64 done, qint64 total) { partProgress(action->index(), done, total); });
connect(action.get(), &NetAction::status, this, &NetJob::status);
} else { } else {
m_todo.append(m_parts_progress.size() - 1); m_todo.append(m_parts_progress.size() - 1);
} }
@ -222,6 +224,7 @@ void NetJob::startMoreParts()
connect(part.get(), &NetAction::failed, this, [this, part](QString){ partFailed(part->index()); }); connect(part.get(), &NetAction::failed, this, [this, part](QString){ partFailed(part->index()); });
connect(part.get(), &NetAction::aborted, this, [this, part]{ partAborted(part->index()); }); connect(part.get(), &NetAction::aborted, this, [this, part]{ partAborted(part->index()); });
connect(part.get(), &NetAction::progress, this, [this, part](qint64 done, qint64 total) { partProgress(part->index(), done, total); }); connect(part.get(), &NetAction::progress, this, [this, part](qint64 done, qint64 total) { partProgress(part->index(), done, total); });
connect(part.get(), &NetAction::status, this, &NetJob::status);
part->startAction(m_network); part->startAction(m_network);
} }

View File

@ -100,6 +100,7 @@ void Task::emitAborted()
m_failReason = "Aborted."; m_failReason = "Aborted.";
qDebug() << "Task" << describe() << "aborted."; qDebug() << "Task" << describe() << "aborted.";
emit aborted(); emit aborted();
emit finished();
} }
void Task::emitSucceeded() void Task::emitSucceeded()

View File

@ -79,7 +79,7 @@ class Task : public QObject {
public slots: public slots:
virtual void start(); virtual void start();
virtual bool abort() { return false; }; virtual bool abort() { if(canAbort()) emitAborted(); return canAbort(); };
protected: protected:
virtual void executeTask() = 0; virtual void executeTask() = 0;