From 12ffad55f771ccb585cf7d1ce9298b077cbdccd1 Mon Sep 17 00:00:00 2001 From: luddens Date: Thu, 2 May 2019 14:12:02 +0200 Subject: [PATCH 1/3] update download's status when we get it --- src/downloader.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/downloader.cpp b/src/downloader.cpp index e962aabef..fe34b9025 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -139,11 +139,13 @@ Download* Downloader::startDownload(const std::string& uri) Download* Downloader::getDownload(const std::string& did) { try { + m_knownDownloads.at(did).get()->updateStatus(true); return m_knownDownloads.at(did).get(); } catch(exception& e) { for (auto gid : mp_aria->tellActive()) { if (gid == did) { m_knownDownloads[gid] = std::unique_ptr(new Download(mp_aria, gid)); + m_knownDownloads.at(gid).get()->updateStatus(true); return m_knownDownloads[gid].get(); } } From ec8f1ffe9ce553f8efc2078736626957e6ee949a Mon Sep 17 00:00:00 2001 From: luddens Date: Mon, 29 Apr 2019 11:25:35 +0200 Subject: [PATCH 2/3] Add pause and unpause functions for aria2 This functions enable to stop and resume download with aria2. The Downloader's constructor now checks the paused downloads with the function "tellWaiting()" to get them at the start of kiwix-desktop. --- include/downloader.h | 2 ++ src/aria2.cpp | 35 ++++++++++++++++++++++++++++++++++- src/aria2.h | 3 +++ src/downloader.cpp | 31 ++++++++++++++++++++++++++++++- 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/include/downloader.h b/include/downloader.h index 1fa439f25..43efee9d9 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -54,6 +54,8 @@ class Download { m_status(K_UNKNOWN), m_did(did) {}; void updateStatus(bool follow=false); + void pauseDownload(); + void resumeDownload(); StatusResult getStatus() { return m_status; } std::string getDid() { return m_did; } std::string getFollowedBy() { return m_followedBy; } diff --git a/src/aria2.cpp b/src/aria2.cpp index 92c4095d1..c24135a85 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -171,7 +171,6 @@ std::vector Aria2::tellActive() MethodCall methodCall("aria2.tellActive", m_secret); auto statusArray = methodCall.newParamValue().getArray(); statusArray.addValue().set(std::string("gid")); - statusArray.addValue().set(std::string("following")); auto responseContent = doRequest(methodCall); MethodResponse response(responseContent); std::vector activeGID; @@ -186,6 +185,27 @@ std::vector Aria2::tellActive() return activeGID; } +std::vector Aria2::tellWaiting() +{ + MethodCall methodCall("aria2.tellWaiting", m_secret); + methodCall.newParamValue().set(0); + methodCall.newParamValue().set(99); // max number of downloads to be returned, don't know how to set this properly assumed that there will not be more than 99 paused downloads. + auto statusArray = methodCall.newParamValue().getArray(); + statusArray.addValue().set(std::string("gid")); + auto responseContent = doRequest(methodCall); + MethodResponse response(responseContent); + std::vector waitingGID; + int index = 0; + while(true) { + try { + auto structNode = response.getParamValue(0).getArray().getValue(index++).getStruct(); + auto gidNode = structNode.getMember("gid"); + waitingGID.push_back(gidNode.getValue().getAsS()); + } catch (InvalidRPCNode& e) { break; } + } + return waitingGID; +} + void Aria2::saveSession() { MethodCall methodCall("aria2.saveSession", m_secret); @@ -199,5 +219,18 @@ void Aria2::shutdown() doRequest(methodCall); } +void Aria2::pause(const std::string& gid) +{ + MethodCall methodCall("aria2.pause", m_secret); + methodCall.newParamValue().set(gid); + doRequest(methodCall); +} + +void Aria2::unpause(const std::string& gid) +{ + MethodCall methodCall("aria2.unpause", m_secret); + methodCall.newParamValue().set(gid); + doRequest(methodCall); +} } // end namespace kiwix diff --git a/src/aria2.h b/src/aria2.h index 16b753ccf..9f073371a 100644 --- a/src/aria2.h +++ b/src/aria2.h @@ -37,8 +37,11 @@ class Aria2 std::string addUri(const std::vector& uri); std::string tellStatus(const std::string& gid, const std::vector& statusKey); std::vector tellActive(); + std::vector tellWaiting(); void saveSession(); void shutdown(); + void pause(const std::string& gid); + void unpause(const std::string& gid); }; }; //end namespace kiwix diff --git a/src/downloader.cpp b/src/downloader.cpp index fe34b9025..5d1e5e482 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -93,6 +93,24 @@ void Download::updateStatus(bool follow) } } +void Download::resumeDownload() +{ + if (!m_followedBy.empty()) + mp_aria->unpause(m_followedBy); + else + mp_aria->unpause(m_did); + updateStatus(true); +} + +void Download::pauseDownload() +{ + if (!m_followedBy.empty()) + mp_aria->pause(m_followedBy); + else + mp_aria->pause(m_did); + updateStatus(true); +} + /* Constructor */ Downloader::Downloader() : mp_aria(new Aria2()) @@ -101,6 +119,10 @@ Downloader::Downloader() : m_knownDownloads[gid] = std::unique_ptr(new Download(mp_aria, gid)); m_knownDownloads[gid]->updateStatus(); } + for (auto gid : mp_aria->tellWaiting()) { + m_knownDownloads[gid] = std::unique_ptr(new Download(mp_aria, gid)); + m_knownDownloads[gid]->updateStatus(); + } } @@ -145,10 +167,17 @@ Download* Downloader::getDownload(const std::string& did) for (auto gid : mp_aria->tellActive()) { if (gid == did) { m_knownDownloads[gid] = std::unique_ptr(new Download(mp_aria, gid)); - m_knownDownloads.at(gid).get()->updateStatus(true); + m_knownDownloads.at(gid).get()->updateStatus(true); return m_knownDownloads[gid].get(); } } + for (auto gid : mp_aria->tellWaiting()) { + if (gid == did) { + m_knownDownloads[gid] = std::unique_ptr(new Download(mp_aria, gid)); + m_knownDownloads.at(gid).get()->updateStatus(true); + return m_knownDownloads[gid].get(); + } + } throw e; } } From 491b6d655b966aa90ad8f1465948cb520abd1446 Mon Sep 17 00:00:00 2001 From: luddens Date: Mon, 29 Apr 2019 17:29:42 +0200 Subject: [PATCH 3/3] add remove fct with aria2 --- include/downloader.h | 1 + src/aria2.cpp | 7 +++++++ src/aria2.h | 1 + src/downloader.cpp | 11 +++++++++++ 4 files changed, 20 insertions(+) diff --git a/include/downloader.h b/include/downloader.h index 43efee9d9..b60e48136 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -56,6 +56,7 @@ class Download { void updateStatus(bool follow=false); void pauseDownload(); void resumeDownload(); + void cancelDownload(); StatusResult getStatus() { return m_status; } std::string getDid() { return m_did; } std::string getFollowedBy() { return m_followedBy; } diff --git a/src/aria2.cpp b/src/aria2.cpp index c24135a85..0366beb90 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -233,4 +233,11 @@ void Aria2::unpause(const std::string& gid) doRequest(methodCall); } +void Aria2::remove(const std::string& gid) +{ + MethodCall methodCall("aria2.remove", m_secret); + methodCall.newParamValue().set(gid); + doRequest(methodCall); +} + } // end namespace kiwix diff --git a/src/aria2.h b/src/aria2.h index 9f073371a..898904a5d 100644 --- a/src/aria2.h +++ b/src/aria2.h @@ -42,6 +42,7 @@ class Aria2 void shutdown(); void pause(const std::string& gid); void unpause(const std::string& gid); + void remove(const std::string& gid); }; }; //end namespace kiwix diff --git a/src/downloader.cpp b/src/downloader.cpp index 5d1e5e482..1eccf38b6 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -36,6 +36,8 @@ namespace kiwix void Download::updateStatus(bool follow) { + if (m_status == Download::K_REMOVED) + return; static std::vector statusKey = {"status", "files", "totalLength", "completedLength", "followedBy", "downloadSpeed", "verifiedLength"}; @@ -111,6 +113,15 @@ void Download::pauseDownload() updateStatus(true); } +void Download::cancelDownload() +{ + if (!m_followedBy.empty()) + mp_aria->remove(m_followedBy); + else + mp_aria->remove(m_did); + m_status = Download::K_REMOVED; +} + /* Constructor */ Downloader::Downloader() : mp_aria(new Aria2())