diff --git a/include/manager.h b/include/manager.h index 99bfea975..0e980b341 100644 --- a/include/manager.h +++ b/include/manager.h @@ -34,6 +34,24 @@ class xml_document; namespace kiwix { +class LibraryManipulator { + public: + virtual ~LibraryManipulator() {} + virtual bool addBookToLibrary(Book book) = 0; +}; + +class DefaultLibraryManipulator : public LibraryManipulator { + public: + DefaultLibraryManipulator(Library* library) : + library(library) {} + virtual ~DefaultLibraryManipulator() {} + bool addBookToLibrary(Book book) { + return library->addBook(book); + } + private: + kiwix::Library* library; +}; + /** * A tool to manage a `Library`. * @@ -43,6 +61,7 @@ namespace kiwix class Manager { public: + Manager(LibraryManipulator* manipulator); Manager(Library* library); ~Manager(); @@ -94,27 +113,6 @@ class Manager */ bool readOpds(const std::string& content, const std::string& urlHost); - /** - * Set the path of the external fulltext index associated to a book. - * - * @param id The id of the book to set. - * @param path The path of the external fullext index. - * @param supportedIndexType The type of the fulltext index. - * @return True if the book is in the library. - */ - bool setBookIndex(const std::string& id, - const std::string& path, - const supportedIndexType type = XAPIAN); - - /** - * Set the path of the zim file associated to a book. - * - * @param id The id of the book to set. - * @param path The path of the zim file. - * @return True if the book is in the library. - */ - bool setBookPath(const std::string& id, const std::string& path); - /** * Add a book to the library. * @@ -201,7 +199,8 @@ class Manager std::vector bookIdList; protected: - kiwix::Library* library; + kiwix::LibraryManipulator* manipulator; + bool mustDeleteManipulator; bool readBookFromPath(const std::string& path, Book* book); bool parseXmlDom(const pugi::xml_document& doc, diff --git a/src/manager.cpp b/src/manager.cpp index e85081dc1..41709ef00 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -25,14 +25,26 @@ namespace kiwix { /* Constructor */ -Manager::Manager(Library* library): +Manager::Manager(LibraryManipulator* manipulator): writableLibraryPath(""), - library(library) + manipulator(manipulator), + mustDeleteManipulator(false) { } + +Manager::Manager(Library* library) : + writableLibraryPath(""), + manipulator(new DefaultLibraryManipulator(library)), + mustDeleteManipulator(true) +{ +} + /* Destructor */ Manager::~Manager() { + if (mustDeleteManipulator) { + delete manipulator; + } } bool Manager::parseXmlDom(const pugi::xml_document& doc, const bool readOnly, @@ -44,24 +56,20 @@ bool Manager::parseXmlDom(const pugi::xml_document& doc, for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) { - bool ok = true; kiwix::Book book; book.setReadOnly(readOnly); - book.updateFromXml(bookNode, removeLastPathElement(libraryPath, true, false)); + book.updateFromXml(bookNode, + removeLastPathElement(libraryPath, true, false)); /* Update the book properties with the new importer */ if (libraryVersion.empty() || atoi(libraryVersion.c_str()) <= atoi(KIWIX_LIBRARY_VERSION)) { - ok = false; if (!book.getPath().empty()) { - ok = this->readBookFromPath(book.getPath(), &book); + this->readBookFromPath(book.getPath(), &book); } } - - if (ok) { - library->addBook(book); - } + manipulator->addBookToLibrary(book); } return true; @@ -114,7 +122,7 @@ bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& url } /* Update the book properties with the new importer */ - library->addBook(book); + manipulator->addBookToLibrary(book); } return true; @@ -188,7 +196,7 @@ std::string Manager::addBookFromPathAndGetId(const std::string& pathToOpen, || (checkMetaData && !book.getTitle().empty() && !book.getLanguage().empty() && !book.getDate().empty())) { book.setUrl(url); - library->addBook(book); + manipulator->addBookToLibrary(book); return book.getId(); } } @@ -223,33 +231,4 @@ bool Manager::readBookFromPath(const std::string& path, kiwix::Book* book) return true; } -bool Manager::setBookIndex(const std::string& id, - const std::string& path, - const supportedIndexType type) -try { - auto book = library->getBookById(id); - book.setIndexPath(isRelativePath(path) - ? computeAbsolutePath( - removeLastPathElement(writableLibraryPath, true, false), - path) - : path); - book.setIndexType(type); - return true; -} catch (...) { - return false; -} - -bool Manager::setBookPath(const std::string& id, const std::string& path) -try { - auto book = library->getBookById(id); - book.setPath(isRelativePath(path) - ? computeAbsolutePath( - removeLastPathElement(writableLibraryPath, true, false), - path) - : path); - return true; -} catch(...) { - return false; -} - }