From 042f3ef55c0b469f438542152c4eb02b0789ea3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 14 Aug 2016 02:33:31 +0200 Subject: GH-352 Make OneSix instance update downloads cancellable --- api/logic/net/NetJob.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'api/logic/net/NetJob.cpp') diff --git a/api/logic/net/NetJob.cpp b/api/logic/net/NetJob.cpp index ca86242a..2e375760 100644 --- a/api/logic/net/NetJob.cpp +++ b/api/logic/net/NetJob.cpp @@ -47,6 +47,15 @@ void NetJob::partFailed(int index) startMoreParts(); } +void NetJob::partAborted(int index) +{ + m_aborted = true; + m_doing.remove(index); + m_failed.insert(index); + downloads[index].get()->disconnect(this); + startMoreParts(); +} + void NetJob::partProgress(int index, qint64 bytesReceived, qint64 bytesTotal) { auto &slot = parts_progress[index]; @@ -85,6 +94,11 @@ void NetJob::startMoreParts() qDebug() << m_job_name << "succeeded."; emitSucceeded(); } + else if(m_aborted) + { + qDebug() << m_job_name << "aborted."; + emitFailed(tr("Job '%1' aborted.").arg(m_job_name)); + } else { qCritical() << m_job_name << "failed."; @@ -104,6 +118,7 @@ void NetJob::startMoreParts() // connect signals :D connect(part.get(), SIGNAL(succeeded(int)), SLOT(partSucceeded(int))); connect(part.get(), SIGNAL(failed(int)), SLOT(partFailed(int))); + connect(part.get(), SIGNAL(aborted(int)), SLOT(partAborted(int))); connect(part.get(), SIGNAL(netActionProgress(int, qint64, qint64)), SLOT(partProgress(int, qint64, qint64))); part->start(); @@ -121,3 +136,37 @@ QStringList NetJob::getFailedFiles() failed.sort(); return failed; } + +bool NetJob::canAbort() const +{ + bool canFullyAbort = true; + // can abort the waiting? + for(auto index: m_todo) + { + auto part = downloads[index]; + canFullyAbort &= part->canAbort(); + } + // can abort the active? + for(auto index: m_doing) + { + auto part = downloads[index]; + canFullyAbort &= part->canAbort(); + } + return canFullyAbort; +} + +bool NetJob::abort() +{ + bool fullyAborted = true; + // fail all waiting + m_failed.unite(m_todo.toSet()); + m_todo.clear(); + // abort active + auto toKill = m_doing.toList(); + for(auto index: toKill) + { + auto part = downloads[index]; + fullyAborted &= part->abort(); + } + return fullyAborted; +} -- cgit v1.2.3