From c10026a3a2616245f5ea47dedf3be436f5e07bc1 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 10 Dec 2023 16:21:44 +0400 Subject: [PATCH] User-controllable catalog host & port So far the details of the host serving the OPDS catalog were hardcoded. In order to debug issues related to large downloads it helps if one can use one's own server. Hence this enhancement. --- src/contentmanager.cpp | 4 ++-- src/opdsrequestmanager.cpp | 25 ++++++++++++++++++++----- src/opdsrequestmanager.h | 4 ++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 26f323d..83370c9 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -681,13 +681,13 @@ void ContentManager::updateLibrary() { } catch (std::runtime_error&) {} } -#define CATALOG_URL "library.kiwix.org" void ContentManager::updateRemoteLibrary(const QString& content) { QtConcurrent::run([=]() { QMutexLocker locker(&remoteLibraryLocker); mp_remoteLibrary = kiwix::Library::create(); kiwix::Manager manager(mp_remoteLibrary); - manager.readOpds(content.toStdString(), CATALOG_URL); + const auto catalogUrl = m_remoteLibraryManager.getCatalogHost(); + manager.readOpds(content.toStdString(), catalogUrl.toStdString()); emit(this->booksChanged()); emit(this->pendingRequest(false)); }); diff --git a/src/opdsrequestmanager.cpp b/src/opdsrequestmanager.cpp index 8a24302..a3346fe 100644 --- a/src/opdsrequestmanager.cpp +++ b/src/opdsrequestmanager.cpp @@ -5,8 +5,22 @@ OpdsRequestManager::OpdsRequestManager() { } -#define CATALOG_HOST "library.kiwix.org" -#define CATALOG_PORT 443 +QString OpdsRequestManager::getCatalogHost() +{ + const char* const envVarVal = getenv("KIWIX_CATALOG_HOST"); + return envVarVal + ? envVarVal + : "library.kiwix.org"; +} + +int OpdsRequestManager::getCatalogPort() +{ + const char* const envVarVal = getenv("KIWIX_CATALOG_PORT"); + return envVarVal + ? atoi(envVarVal) + : 443; +} + void OpdsRequestManager::doUpdate(const QString& currentLanguage, const QString& categoryFilter) { QUrlQuery query; @@ -36,9 +50,10 @@ void OpdsRequestManager::doUpdate(const QString& currentLanguage, const QString& QNetworkReply* OpdsRequestManager::opdsResponseFromPath(const QString &path, const QUrlQuery &query) { QUrl url; - url.setScheme("https"); - url.setHost(CATALOG_HOST); - url.setPort(CATALOG_PORT); + const int port = getCatalogPort(); + url.setScheme(port == 443 ? "https" : "http"); + url.setHost(getCatalogHost()); + url.setPort(port); url.setPath(path); url.setQuery(query); qInfo() << "Downloading" << url.toString(QUrl::FullyEncoded); diff --git a/src/opdsrequestmanager.h b/src/opdsrequestmanager.h index 5a3d3c2..b14f9a2 100644 --- a/src/opdsrequestmanager.h +++ b/src/opdsrequestmanager.h @@ -32,6 +32,10 @@ public slots: void receiveContent(QNetworkReply*); void receiveLanguages(QNetworkReply*); void receiveCategories(QNetworkReply*); + +public: + static QString getCatalogHost(); + static int getCatalogPort(); }; #endif // OPDSREQUESTMANAGER_H