diff options
Diffstat (limited to 'api/logic/net/NetJob.cpp')
-rw-r--r-- | api/logic/net/NetJob.cpp | 82 |
1 files changed, 60 insertions, 22 deletions
diff --git a/api/logic/net/NetJob.cpp b/api/logic/net/NetJob.cpp index 275da749..f4fd7245 100644 --- a/api/logic/net/NetJob.cpp +++ b/api/logic/net/NetJob.cpp @@ -18,22 +18,24 @@ #include <QDebug> -void NetJob::partSucceeded(int index) +void NetJob::partSucceeded() { + auto index = m_partsIndex[(NetAction *)QObject::sender()]; // do progress. all slots are 1 in size at least - auto &slot = parts_progress[index]; - partProgress(index, slot.total_progress, slot.total_progress); + auto &slot = m_parts[index]; + setPartProgress(index, slot.total_progress, slot.total_progress); m_doing.remove(index); m_done.insert(index); - downloads[index].get()->disconnect(this); + slot.download->disconnect(this); startMoreParts(); } -void NetJob::partFailed(int index) +void NetJob::partFailed() { + auto index = m_partsIndex[(NetAction *)QObject::sender()]; m_doing.remove(index); - auto &slot = parts_progress[index]; + auto &slot = m_parts[index]; if (slot.failures == 3) { m_failed.insert(index); @@ -43,22 +45,30 @@ void NetJob::partFailed(int index) slot.failures++; m_todo.enqueue(index); } - downloads[index].get()->disconnect(this); + slot.download->disconnect(this); startMoreParts(); } -void NetJob::partAborted(int index) +void NetJob::partAborted() { + auto index = m_partsIndex[(NetAction *)QObject::sender()]; m_aborted = true; m_doing.remove(index); + auto &slot = m_parts[index]; m_failed.insert(index); - downloads[index].get()->disconnect(this); + slot.download->disconnect(this); startMoreParts(); } -void NetJob::partProgress(int index, qint64 bytesReceived, qint64 bytesTotal) +void NetJob::partProgress(qint64 bytesReceived, qint64 bytesTotal) { - auto &slot = parts_progress[index]; + auto index = m_partsIndex[(NetAction *)QObject::sender()]; + setPartProgress(index, bytesReceived, bytesTotal); +} + +void NetJob::setPartProgress(int index, qint64 bytesReceived, qint64 bytesTotal) +{ + auto &slot = m_parts[index]; current_progress -= slot.current_progress; slot.current_progress = bytesReceived; @@ -74,7 +84,7 @@ void NetJob::executeTask() { qDebug() << m_job_name.toLocal8Bit() << " started."; m_running = true; - for (int i = 0; i < downloads.size(); i++) + for (int i = 0; i < m_parts.size(); i++) { m_todo.enqueue(i); } @@ -114,24 +124,28 @@ void NetJob::startMoreParts() return; int doThis = m_todo.dequeue(); m_doing.insert(doThis); - auto part = downloads[doThis]; + auto part = m_parts[doThis].download; // 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))); + connectAction(part.get()); part->start(); } } +void NetJob::connectAction(NetAction* action) +{ + connect(action, &NetAction::succeeded, this, &NetJob::partSucceeded); + connect(action, &NetAction::failed, this, &NetJob::partFailed); + connect(action, &NetAction::aborted, this, &NetJob::partAborted); + connect(action, &NetAction::progress, this, &NetJob::partProgress); +} + QStringList NetJob::getFailedFiles() { QStringList failed; for (auto index: m_failed) { - failed.push_back(downloads[index]->m_url.toString()); + failed.push_back(m_parts[index].download->m_url.toString()); } failed.sort(); return failed; @@ -143,13 +157,13 @@ bool NetJob::canAbort() const // can abort the waiting? for(auto index: m_todo) { - auto part = downloads[index]; + auto part = m_parts[index].download; canFullyAbort &= part->canAbort(); } // can abort the active? for(auto index: m_doing) { - auto part = downloads[index]; + auto part = m_parts[index].download; canFullyAbort &= part->canAbort(); } return canFullyAbort; @@ -165,8 +179,32 @@ bool NetJob::abort() auto toKill = m_doing.toList(); for(auto index: toKill) { - auto part = downloads[index]; + auto part = m_parts[index].download; fullyAborted &= part->abort(); } return fullyAborted; } + +void NetJob::addNetAction(NetActionPtr action) +{ + m_partsIndex[action.get()] = m_parts.count(); + part_info pi; + { + pi.current_progress = action->getProgress(); + pi.total_progress = action->getTotalProgress(); + pi.failures = 0; + pi.download = action; + } + m_parts.append(pi); + + total_progress += pi.total_progress; + current_progress += pi.current_progress; + + // if this is already running, the action needs to be started right away! + if (isRunning()) + { + setProgress(current_progress, total_progress); + connectAction(action.get()); + action->start(); + } +} |