From fe9efe84ec00555561a20d4a4dc472d82b0b9eb1 Mon Sep 17 00:00:00 2001 From: Hamazasp Avetisyan Date: Tue, 18 Aug 2026 13:01:36 +0400 Subject: [PATCH] Add readOnly parameter to Manager::readOpds() --- include/manager.h | 19 +++++++++++-------- src/manager.cpp | 13 ++++++++----- test/manager.cpp | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/include/manager.h b/include/manager.h index 672a6b2a..9e77beea 100644 --- a/include/manager.h +++ b/include/manager.h @@ -68,9 +68,10 @@ class Manager explicit Manager(LibraryPtr library); /** - * Read a `library.xml` and add book in the file to the library. + * Read a library XML or an OPDS file and add the books in the file to the + * library. * - * @param path The (utf8) path to the `library.xml`. + * @param path The (utf8) path to the library file. * @param readOnly Set if the libray path could be overwritten latter with * updated content. * @param trustLibrary use book metadata coming from XML. @@ -104,7 +105,7 @@ class Manager bool trustLibrary = true); /** - * Load a library content stored in a OPDS stream. + * Load a library content stored in a OPDS stream or a local library file. * * @param content The content of the OPDS stream. * @param urlHost The host used to resolve relative acquisition/thumbnail @@ -113,7 +114,9 @@ class Manager * prepending the urlHost string to the href value of the link. * @return True if the content has been properly parsed. */ - bool readOpds(const std::string& content, const std::string& urlHost); + bool readOpds(const std::string& content, + const std::string& urlHost, + bool readOnly = false); /** @@ -158,7 +161,7 @@ class Manager /** * Add all books from the directory tree into the library. - * + * * @param path The path of the directory to scan. * @param verboseFlag Verbose logs flag. */ @@ -181,9 +184,9 @@ class Manager const std::string& libraryPath, bool trustLibrary); bool parseOpdsDom(const pugi::xml_document& doc, - const std::string& urlHost); - + const std::string& urlHost, + bool readOnly); }; -} +} // namespace kiwix #endif diff --git a/src/manager.cpp b/src/manager.cpp index a5df1e60..e39e8a3d 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -142,7 +142,9 @@ bool Manager::readXml(const std::string& xml, -bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& urlHost) +bool Manager::parseOpdsDom(const pugi::xml_document& doc, + const std::string& urlHost, + bool readOnly) { pugi::xml_node libraryNode = doc.child("feed"); if (!libraryNode) { @@ -169,7 +171,7 @@ bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& url entryNode = entryNode.next_sibling("entry")) { kiwix::Book book; - book.setReadOnly(false); + book.setReadOnly(readOnly); book.updateFromOpds(entryNode, urlHost); /* Update the book properties with the new importer */ @@ -181,15 +183,16 @@ bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& url -bool Manager::readOpds(const std::string& content, const std::string& urlHost) +bool Manager::readOpds(const std::string& content, + const std::string& urlHost, + bool readOnly) { pugi::xml_document doc; pugi::xml_parse_result result = doc.load_buffer((void*)content.data(), content.size()); if (result) { - this->parseOpdsDom(doc, urlHost); - return true; + return this->parseOpdsDom(doc, urlHost, readOnly); } return false; diff --git a/test/manager.cpp b/test/manager.cpp index 5fb80c61..05e1cf40 100644 --- a/test/manager.cpp +++ b/test/manager.cpp @@ -246,6 +246,20 @@ const char sampleOpdsFeed[] = R"( )"; +TEST(ManagerTest, readOpdsHonorsReadOnlyTrue) +{ + // readOpds() defaults to readOnly=false (see + // ManagerTest.readOpdsAddsEntriesAndParsesSearchMetadata below, which + // covers that case) - this checks that readOnly=true is honored too. + auto lib = kiwix::Library::create(); + kiwix::Manager manager(lib); + + EXPECT_TRUE(manager.readOpds(sampleOpdsFeed, "http://example.com", /*readOnly=*/true)); + + EXPECT_TRUE(lib->getBookById("book1").readOnly()); + EXPECT_TRUE(lib->getBookById("book2").readOnly()); +} + TEST(ManagerTest, readOpdsAddsEntriesAndParsesSearchMetadata) { auto lib = kiwix::Library::create();