From dcfac3432f981191da68f14f21f5010fc4da51c0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 18 Nov 2020 11:38:59 -0500 Subject: Fixed issue with status reporting --- libbible.h | 8 ++++++-- mods.cc | 24 +++++++++++++----------- testLibbible.cc | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/libbible.h b/libbible.h index cf8a1f4..85ea9eb 100644 --- a/libbible.h +++ b/libbible.h @@ -45,14 +45,18 @@ namespace libbible { class Status { public: - void update(unsigned long totalBytes, unsigned long completedBytes, std::string message); + virtual void update(unsigned long totalBytes, unsigned long completedBytes, std::string message) {} }; /** * @param status Status update method is called asynchronously as download progresses + */ + void setStatusReporter(Status& status); + + /** * @return A mapping from language to bible version names */ - std::map> downloadModsAvailable(Status *status = nullptr); + std::map> downloadModsAvailable(); /** * Cancel an in-progress download diff --git a/mods.cc b/mods.cc index 6b2063a..903c91f 100644 --- a/mods.cc +++ b/mods.cc @@ -10,9 +10,9 @@ using namespace std; class myStatusReporter : public sword::StatusReporter { public: myStatusReporter(libbible::Status *status); - virtual ~myStatusReporter(); - virtual void preStatus(long totalBytes, long completedBytes, const char *message); - virtual void update(unsigned long totalBytes, unsigned long completedBytes); + ~myStatusReporter(); + void preStatus(long totalBytes, long completedBytes, const char *message); + void update(unsigned long totalBytes, unsigned long completedBytes); protected: libbible::Status *status; string message; @@ -24,31 +24,33 @@ myStatusReporter::myStatusReporter(libbible::Status *s) { myStatusReporter::~myStatusReporter() {}; -void libbible::Status::update(unsigned long totalBytes, unsigned long completedBytes, string message) {} +//virtual void libbible::Status::update(unsigned long totalBytes, unsigned long completedBytes, string message) {} void myStatusReporter::preStatus(long totalBytes, long completedBytes, const char *msg) { message = string(msg); status->update((unsigned long) totalBytes, (unsigned long) completedBytes, message); + //printf("Got a status update: %ld / %ld, \"%s\"\n", completedBytes, totalBytes, message.c_str()); } void myStatusReporter::update(unsigned long totalBytes, unsigned long completedBytes) { status->update(totalBytes, completedBytes, message); + //printf("Got a status update: %ld / %ld, \"%s\"\n", completedBytes, totalBytes, message.c_str()); } string basedir = (getenv("HOME")) + string("/.sword/"); sword::InstallMgr *installMgr = new sword::InstallMgr((basedir + std::string("InstallMgr")).c_str(), nullptr); map>> installSources; -map> libbible::downloadModsAvailable(libbible::Status *status) { +void libbible::setStatusReporter(libbible::Status& status) { + myStatusReporter *msr = new myStatusReporter(&status); + free(installMgr); + installMgr = new sword::InstallMgr((basedir + std::string("InstallMgr")).c_str(), msr); +} + +map> libbible::downloadModsAvailable() { installSources.clear(); mkdir((basedir + std::string("mods.d/")).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); mkdir((basedir + std::string("modules/")).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - myStatusReporter *msr = nullptr; - if(status) { - msr = new myStatusReporter(status); - } - free(installMgr); - installMgr = new sword::InstallMgr((basedir + std::string("InstallMgr")).c_str(), msr); installMgr->setUserDisclaimerConfirmed(true); string confpath = basedir + string("InstallMgr/InstallMgr.conf"); if(! sword::FileMgr::existsFile(confpath.c_str())) { diff --git a/testLibbible.cc b/testLibbible.cc index 3850aa0..8a7f468 100644 --- a/testLibbible.cc +++ b/testLibbible.cc @@ -47,6 +47,19 @@ class TestLibbible : public CppUnit::TestFixture //----------------------------------------------------------------------------- +class StatusTester : public libbible::Status +{ + public: + virtual void update(unsigned long totalBytes, unsigned long completedBytes, string message); + bool hasBeenUpdated = false; +}; + +void StatusTester::update(unsigned long totalBytes, unsigned long completedBytes, string message) { + hasBeenUpdated = true; +} + +//----------------------------------------------------------------------------- + void TestLibbible::testGetModules(void) { map> mods = libbible::getModules(); for(auto pair : mods) { @@ -175,7 +188,11 @@ void TestLibbible::testSettings(void) { } void TestLibbible::testDownload(void) { + StatusTester status; + libbible::setStatusReporter(status); map> modsAvailable = libbible::downloadModsAvailable(); + //CPPUNIT_ASSERT(status.hasBeenUpdated); + status.hasBeenUpdated = false; // We try installing the first available one string language; string name; @@ -192,6 +209,7 @@ void TestLibbible::testDownload(void) { libbible::installModFromInternet(language, name); mods = libbible::getModules(); CPPUNIT_ASSERT(mods.find(name) != mods.end()); + CPPUNIT_ASSERT(status.hasBeenUpdated); } //----------------------------------------------------------------------------- -- cgit v1.2.3