diff options
author | Your Name <you@example.com> | 2020-11-05 15:50:11 -0500 |
---|---|---|
committer | Your Name <you@example.com> | 2020-11-05 15:50:11 -0500 |
commit | 2fe897e2cf750339a7e466aeafe64f45fb650f10 (patch) | |
tree | d27bbda7c1ef5368a90704843deea014e6c810de /mods.cc | |
download | libbible-2fe897e2cf750339a7e466aeafe64f45fb650f10.tar.gz libbible-2fe897e2cf750339a7e466aeafe64f45fb650f10.tar.bz2 libbible-2fe897e2cf750339a7e466aeafe64f45fb650f10.zip |
Initial commit
Diffstat (limited to 'mods.cc')
-rw-r--r-- | mods.cc | 126 |
1 files changed, 126 insertions, 0 deletions
@@ -0,0 +1,126 @@ +#include "libbible.h" +#include <sword/swmgr.h> +#include <sword/swmodule.h> +#include <sword/installmgr.h> +#include <sword/filemgr.h> +#include <sword/remotetrans.h> + +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); + protected: + libbible::Status *status; + string message; +}; + +myStatusReporter::myStatusReporter(libbible::Status *s) { + status = s; +} + +myStatusReporter::~myStatusReporter() {}; + +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); +} + +void myStatusReporter::update(unsigned long totalBytes, unsigned long completedBytes) { + status->update(totalBytes, completedBytes, message); +} + +string basedir = (getenv("HOME")) + string("/.sword/"); +sword::InstallMgr *installMgr = new sword::InstallMgr((basedir + std::string("InstallMgr")).c_str(), nullptr); +map<string, vector<pair<string, sword::InstallSource *>>> installSources; + +map<string, vector<string>> libbible::downloadModsAvailable(libbible::Status *status) { + 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())) { + // Lifted directly from xiphos + sword::FileMgr::createParent(confpath.c_str()); + sword::SWConfig config(confpath.c_str()); + sword::InstallSource is("FTP"); + is.caption = "CrossWire"; + is.source = "ftp.crosswire.org"; + is.directory = "/pub/sword/raw"; + config["General"]["PassiveFTP"] = "true"; + config["Sources"]["FTPSource"] = is.getConfEnt(); + config.save(); + } + installMgr->readInstallConf(); + installMgr->refreshRemoteSourceConfiguration(); + map<string, vector<string>> modsAvailable; + //printf("Getting langs...\n"); + for(auto src : installMgr->sources) { + if(src.second->getMgr()->Modules.empty()) { + //printf("Refreshing remote source: %s\n", src.second->getConfEnt().c_str()); + installMgr->refreshRemoteSource(src.second); + } + for(auto mod : src.second->getMgr()->Modules) { + auto *curMod = mod.second; + string type(curMod->getType()); + if(type == "Biblical Texts") { + string language(curMod->getLanguage()); + //printf("Got language %s\n", language.c_str()); + vector<string> newMods; + vector<pair<string, sword::InstallSource *>> newSources; + // emplace only adds if key is unique + modsAvailable.emplace(language, newMods); + installSources.emplace(language, newSources); + modsAvailable[language].push_back(string(curMod->getName())); + pair<string, sword::InstallSource *> p(string(curMod->getName()), src.second); + installSources[language].push_back(p); + } + } + } + return modsAvailable; +} + +void libbible::terminateDownload() { + installMgr->terminate(); +} + +void libbible::installModFromInternet(string language, string name) { + // Searching through map<string, vector<pair<string, sword::InstallSource *>>> installSources; + for (pair<string, sword::InstallSource *> p : installSources[language]) { + if(p.first == name) { + sword::SWMgr mgr(basedir.c_str()); + installMgr->installModule(&mgr, 0, name.c_str(), p.second); + break; + } + } +} + +void libbible::installModFromZip(string filename) { + // So... turns out it's a mite unsupported to install from a .zip + // Here's the deal. We do a syscall to unzip. We fancy like that. + // TODO: Use the ZipCompress module from SWORD instead. + string command = "unzip -o " + filename + " -d " + basedir + "&> /dev/null"; + if(system(command.c_str())) { + //Uh oh... + printf("Something bad happened when unpacking %s\n", filename.c_str()); + } + +} + +void libbible::uninstallMod(string modname) { + sword::SWMgr mgr(basedir.c_str()); + sword::ModMap::iterator it = mgr.Modules.find(modname.c_str()); + installMgr->removeModule(&mgr, it->second->getName()); +} |