From 9fd8e81de21dceee1ff66bef9dc0df6970752635 Mon Sep 17 00:00:00 2001 From: sgourdas Date: Sun, 11 Aug 2024 23:46:34 +0300 Subject: [PATCH] Remove getDataDirectory --- include/downloader.h | 7 ++++++- include/tools.h | 26 ----------------------- src/aria2.cpp | 6 ++---- src/aria2.h | 3 +-- src/downloader.cpp | 4 ++-- src/tools/pathTools.cpp | 46 ----------------------------------------- 6 files changed, 11 insertions(+), 81 deletions(-) diff --git a/include/downloader.h b/include/downloader.h index 8342c4f40..cf42e726f 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -172,7 +172,12 @@ class Downloader typedef std::vector> Options; public: // functions - Downloader(); + /* + * Create a new Downloader object. + * + * @param sessionFileDir: The directory where aria2 will store its session file. + */ + explicit Downloader(std::string sessionFileDir); virtual ~Downloader(); void close(); diff --git a/include/tools.h b/include/tools.h index 13931e876..52a6af661 100644 --- a/include/tools.h +++ b/include/tools.h @@ -45,32 +45,6 @@ typedef std::vector FeedCategories; */ std::string getCurrentDirectory(); -/** - * Return the data directory - * - * The data directory is the default directory where downloaded files - * should be saved (it can be overriden via the options parameter of - * `kiwix::Downloader::startDownload()`). - * - * Its path can vary and is determined as follows: - * - * * `$KIWIX_DATA_DIR` if `$KIWIX_DATA_DIR` environment variable set, *otherwise...* - * * On Windows: - * - * * `$APPDATA/kiwix` if environment variable `$APPDATA` set, *otherwise...* - * * `$USERPROFILE/kiwix` if environment variable `$USERPROFILE` set, *otherwise...* - * - * * On other Operating Systems: - * - * * `$XDG_DATA_HOME/kiwix` if environment variable `$XDG_DATA_HOME` set, *otherwise...* - * * `$HOME/.local/share/kiwx` if environment variable `$HOME` set, *otherwise...* - * - * * Current working directory. - * - * @return the path of the data directory (UTF-8 encoded) - */ -std::string getDataDirectory(); - /** Return the path of the executable * * Some application may be packaged in auto extractible archive (Appimage) and the diff --git a/src/aria2.cpp b/src/aria2.cpp index f2725c9fd..d7fbbf84c 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -55,17 +55,15 @@ void pauseAnyActiveDownloads(const std::string& ariaSessionFilePath) } // unnamed namespace -Aria2::Aria2(): +Aria2::Aria2(std::string sessionFileDir): mp_aria(nullptr), m_port(42042), m_secret(getNewRpcSecret()) { - m_downloadDir = getDataDirectory(); - makeDirectory(m_downloadDir); std::vector callCmd; std::string rpc_port = "--rpc-listen-port=" + to_string(m_port); - std::string session_file = appendToDirectory(getDataDirectory(), "kiwix.session"); + std::string session_file = appendToDirectory(sessionFileDir, "kiwix.session"); pauseAnyActiveDownloads(session_file); std::string session = "--save-session=" + session_file; std::string inputFile = "--input-file=" + session_file; diff --git a/src/aria2.h b/src/aria2.h index f6cd633b8..e7b5ed259 100644 --- a/src/aria2.h +++ b/src/aria2.h @@ -22,11 +22,10 @@ class Aria2 std::unique_ptr mp_aria; int m_port; std::string m_secret; - std::string m_downloadDir; std::string doRequest(const MethodCall& methodCall); public: - Aria2(); + explicit Aria2(std::string sessionFileDir); virtual ~Aria2() = default; void close(); diff --git a/src/downloader.cpp b/src/downloader.cpp index ffc9d321e..380523f5e 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -125,8 +125,8 @@ void Download::cancelDownload() } /* Constructor */ -Downloader::Downloader() : - mp_aria(new Aria2()) +Downloader::Downloader(std::string sessionFileDir) : + mp_aria(new Aria2(sessionFileDir)) { try { for (auto gid : mp_aria->tellWaiting()) { diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index 54f513b0a..a082e33af 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -438,52 +438,6 @@ std::string kiwix::getCurrentDirectory() return ret; } -std::string kiwix::getDataDirectory() -{ -// Try to get the dataDir from the `KIWIX_DATA_DIR` env var -#ifdef _WIN32 - wchar_t* cDataDir = ::_wgetenv(L"KIWIX_DATA_DIR"); - if (cDataDir != nullptr) { - return WideToUtf8(cDataDir); - } -#else - char* cDataDir = ::getenv("KIWIX_DATA_DIR"); - if (cDataDir != nullptr) { - return cDataDir; - } -#endif - -// Compute the dataDir from the user directory. - std::string dataDir; -#ifdef _WIN32 - cDataDir = ::_wgetenv(L"APPDATA"); - if (cDataDir == nullptr) - cDataDir = ::_wgetenv(L"USERPROFILE"); - if (cDataDir != nullptr) - dataDir = WideToUtf8(cDataDir); -#else - cDataDir = ::getenv("XDG_DATA_HOME"); - if (cDataDir != nullptr) { - dataDir = cDataDir; - } else { - cDataDir = ::getenv("HOME"); - if (cDataDir != nullptr) { - dataDir = cDataDir; - dataDir = appendToDirectory(dataDir, ".local"); - dataDir = appendToDirectory(dataDir, "share"); - } - } -#endif - if (!dataDir.empty()) { - dataDir = appendToDirectory(dataDir, "kiwix"); - makeDirectory(dataDir); - return dataDir; - } - -// Let's use the currentDirectory - return getCurrentDirectory(); -} - static std::map extMimeTypes = { { "html", "text/html"}, { "htm", "text/html"},