Add a LibraryManipulator.

Library client (kiwix-desktop) need to know when a book is added to
library by the manager. By using a LibraryManipulator, we can do
dependency injection.
This commit is contained in:
Matthieu Gautier 2018-10-16 17:46:00 +02:00
parent 8176a6eded
commit f718c4c472
2 changed files with 41 additions and 63 deletions

View File

@ -34,6 +34,24 @@ class xml_document;
namespace kiwix 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`. * A tool to manage a `Library`.
* *
@ -43,6 +61,7 @@ namespace kiwix
class Manager class Manager
{ {
public: public:
Manager(LibraryManipulator* manipulator);
Manager(Library* library); Manager(Library* library);
~Manager(); ~Manager();
@ -94,27 +113,6 @@ class Manager
*/ */
bool readOpds(const std::string& content, const std::string& urlHost); 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. * Add a book to the library.
* *
@ -201,7 +199,8 @@ class Manager
std::vector<std::string> bookIdList; std::vector<std::string> bookIdList;
protected: protected:
kiwix::Library* library; kiwix::LibraryManipulator* manipulator;
bool mustDeleteManipulator;
bool readBookFromPath(const std::string& path, Book* book); bool readBookFromPath(const std::string& path, Book* book);
bool parseXmlDom(const pugi::xml_document& doc, bool parseXmlDom(const pugi::xml_document& doc,

View File

@ -25,14 +25,26 @@
namespace kiwix namespace kiwix
{ {
/* Constructor */ /* Constructor */
Manager::Manager(Library* library): Manager::Manager(LibraryManipulator* manipulator):
writableLibraryPath(""), writableLibraryPath(""),
library(library) manipulator(manipulator),
mustDeleteManipulator(false)
{ {
} }
Manager::Manager(Library* library) :
writableLibraryPath(""),
manipulator(new DefaultLibraryManipulator(library)),
mustDeleteManipulator(true)
{
}
/* Destructor */ /* Destructor */
Manager::~Manager() Manager::~Manager()
{ {
if (mustDeleteManipulator) {
delete manipulator;
}
} }
bool Manager::parseXmlDom(const pugi::xml_document& doc, bool Manager::parseXmlDom(const pugi::xml_document& doc,
const bool readOnly, const bool readOnly,
@ -44,24 +56,20 @@ bool Manager::parseXmlDom(const pugi::xml_document& doc,
for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode;
bookNode = bookNode.next_sibling("book")) { bookNode = bookNode.next_sibling("book")) {
bool ok = true;
kiwix::Book book; kiwix::Book book;
book.setReadOnly(readOnly); 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 */ /* Update the book properties with the new importer */
if (libraryVersion.empty() if (libraryVersion.empty()
|| atoi(libraryVersion.c_str()) <= atoi(KIWIX_LIBRARY_VERSION)) { || atoi(libraryVersion.c_str()) <= atoi(KIWIX_LIBRARY_VERSION)) {
ok = false;
if (!book.getPath().empty()) { if (!book.getPath().empty()) {
ok = this->readBookFromPath(book.getPath(), &book); this->readBookFromPath(book.getPath(), &book);
} }
} }
manipulator->addBookToLibrary(book);
if (ok) {
library->addBook(book);
}
} }
return true; 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 */ /* Update the book properties with the new importer */
library->addBook(book); manipulator->addBookToLibrary(book);
} }
return true; return true;
@ -188,7 +196,7 @@ std::string Manager::addBookFromPathAndGetId(const std::string& pathToOpen,
|| (checkMetaData && !book.getTitle().empty() && !book.getLanguage().empty() || (checkMetaData && !book.getTitle().empty() && !book.getLanguage().empty()
&& !book.getDate().empty())) { && !book.getDate().empty())) {
book.setUrl(url); book.setUrl(url);
library->addBook(book); manipulator->addBookToLibrary(book);
return book.getId(); return book.getId();
} }
} }
@ -223,33 +231,4 @@ bool Manager::readBookFromPath(const std::string& path, kiwix::Book* book)
return true; 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;
}
} }