From 3aeeeeee763053a412c3f9bea47f9821f756d13f Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sat, 27 Nov 2021 21:40:12 +0400 Subject: [PATCH] Manager::reload() --- include/manager.h | 15 ++++++++++++++- src/manager.cpp | 14 ++++++++++++++ test/manager.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/include/manager.h b/include/manager.h index f80421b73..13fe19f10 100644 --- a/include/manager.h +++ b/include/manager.h @@ -59,7 +59,10 @@ class LibraryManipulator */ class Manager { - public: + public: // types + typedef std::vector Paths; + + public: // functions explicit Manager(LibraryManipulator* manipulator); explicit Manager(Library* library); @@ -69,10 +72,20 @@ class Manager * @param path The (utf8) path to the `library.xml`. * @param readOnly Set if the libray path could be overwritten latter with * updated content. + * @param trustLibrary use book metadata coming from XML. * @return True if file has been properly parsed. */ bool readFile(const std::string& path, bool readOnly = true, bool trustLibrary = true); + /** + * Sync the contents of the library with one or more `library.xml` files. + * + * The metadata of the library files is trusted unconditionally. + * + * @param paths The (utf8) paths to the `library.xml` files. + */ + void reload(const Paths& paths); + /** * Load a library content store in the string. * diff --git a/src/manager.cpp b/src/manager.cpp index f44ec9b53..b65353542 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -289,4 +289,18 @@ bool Manager::readBookmarkFile(const std::string& path) return true; } +void Manager::reload(const Paths& paths) +{ + for (std::string path : paths) { + if (!path.empty()) { + if ( kiwix::isRelativePath(path) ) + path = kiwix::computeAbsolutePath(kiwix::getCurrentDirectory(), path); + + if (!readFile(path, false, true)) { + throw std::runtime_error("Failed to load the XML library file '" + path + "'."); + } + } + } +} + } diff --git a/test/manager.cpp b/test/manager.cpp index 09647c766..34774f630 100644 --- a/test/manager.cpp +++ b/test/manager.cpp @@ -67,3 +67,29 @@ TEST(ManagerTest, readXml) EXPECT_EQ(45U, book.getMediaCount()); EXPECT_EQ(678U*1024, book.getSize()); } + +TEST(Manager, reload) +{ + kiwix::Library lib; + kiwix::Manager manager(&lib); + + manager.reload({ "./test/library.xml" }); + EXPECT_EQ(lib.getBooksIds(), (kiwix::Library::BookIdCollection{ + "charlesray", + "raycharles", + "raycharles_uncategorized" + })); + + lib.removeBookById("raycharles"); + EXPECT_EQ(lib.getBooksIds(), (kiwix::Library::BookIdCollection{ + "charlesray", + "raycharles_uncategorized" + })); + + manager.reload({ "./test/library.xml" }); + EXPECT_EQ(lib.getBooksIds(), kiwix::Library::BookIdCollection({ + "charlesray", + "raycharles", + "raycharles_uncategorized" + })); +}