diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index 033df35d5..5dc83d0bc 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -18,7 +18,6 @@ */ #include "manager.h" -#include namespace kiwix { @@ -31,51 +30,70 @@ namespace kiwix { Manager::~Manager() { } + bool Manager::parseXmlDom(const pugi::xml_document &doc, const bool readOnly) { + pugi::xml_node libraryNode = doc.child("library"); + library.current = libraryNode.attribute("current").value(); + string libraryVersion = libraryNode.attribute("version").value(); + + for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) { + bool ok = true; + kiwix::Book book; + + book.readOnly = readOnly; + book.id = bookNode.attribute("id").value(); + book.path = bookNode.attribute("path").value(); + book.last = (std::string(bookNode.attribute("last").value()) != "undefined" ? bookNode.attribute("last").value() : ""); + book.indexPath = bookNode.attribute("indexPath").value(); + book.indexType = (std::string(bookNode.attribute("indexType").value()) == "xapian" ? XAPIAN : CLUCENE); + book.title = bookNode.attribute("title").value(); + book.description = bookNode.attribute("description").value(); + book.language = bookNode.attribute("language").value(); + book.date = bookNode.attribute("date").value(); + book.creator = bookNode.attribute("creator").value(); + book.url = bookNode.attribute("url").value(); + book.articleCount = bookNode.attribute("articleCount").value(); + book.mediaCount = bookNode.attribute("mediaCount").value(); + book.size = bookNode.attribute("size").value(); + + /* Update the book properties with the new importer */ + if (libraryVersion.empty() || atoi(libraryVersion.c_str()) < atoi(KIWIX_LIBRARY_VERSION)) { + if (!book.path.empty()) + ok = this->readBookFromPath(book.path, book); + } + + + if (ok) { + library.addBook(book); + } + } + + return true; + } + + bool Manager::readXml(const string xml, const bool readOnly) { + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load_buffer_inplace((void*)xml.data(), xml.size()); + + if (result) { + this->parseXmlDom(doc, readOnly); + } + + return true; + } + bool Manager::readFile(const string path, const bool readOnly) { pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(path.c_str()); if (result) { - pugi::xml_node libraryNode = doc.child("library"); - library.current = libraryNode.attribute("current").value(); - string libraryVersion = libraryNode.attribute("version").value(); - bool ok = true; - - for (pugi::xml_node bookNode = libraryNode.child("book"); bookNode; bookNode = bookNode.next_sibling("book")) { - kiwix::Book book; - book.readOnly = readOnly; - book.id = bookNode.attribute("id").value(); - book.path = bookNode.attribute("path").value(); - book.last = (std::string(bookNode.attribute("last").value()) != "undefined" ? bookNode.attribute("last").value() : ""); - book.indexPath = bookNode.attribute("indexPath").value(); - book.indexType = (std::string(bookNode.attribute("indexType").value()) == "xapian" ? XAPIAN : CLUCENE); - book.title = bookNode.attribute("title").value(); - book.description = bookNode.attribute("description").value(); - book.language = bookNode.attribute("language").value(); - book.date = bookNode.attribute("date").value(); - book.creator = bookNode.attribute("creator").value(); - book.url = bookNode.attribute("url").value(); - book.articleCount = bookNode.attribute("articleCount").value(); - book.mediaCount = bookNode.attribute("mediaCount").value(); - book.size = bookNode.attribute("size").value(); - - /* Update the book properties with the new importer */ - if (libraryVersion.empty() || atoi(libraryVersion.c_str()) < atoi(KIWIX_LIBRARY_VERSION)) { - if (!book.path.empty()) - ok = this->readBookFromPath(book.path, book); - } - - if (ok) { - library.addBook(book); - } - } + // this->parseXmlDom(doc, readOnly); } if (!readOnly) { this->writableLibraryPath = path; } - return result; + return true; } bool Manager::writeFile(const string path) { @@ -259,6 +277,13 @@ namespace kiwix { return false; } + void Manager::removeBookPaths() { + std::vector::iterator itr; + for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { + itr->path = ""; + } + } + bool Manager::listBooks(const supportedListMode mode) { this->bookIdList.clear(); std::vector::iterator itr; diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index 63343b688..a8233112e 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -29,6 +29,8 @@ #include #include +#include + #include #include @@ -45,6 +47,7 @@ namespace kiwix { ~Manager(); bool readFile(const string path, const bool readOnly = true); + bool readXml(const string xml, const bool readOnly = true); bool writeFile(const string path); bool removeBookByIndex(const unsigned int bookIndex); bool removeBookById(const string id); @@ -55,6 +58,7 @@ namespace kiwix { Library cloneLibrary(); bool getBookById(const string id, Book &book); bool updateBookLastOpenDateById(const string id); + void removeBookPaths(); bool listBooks(const supportedListMode); string writableLibraryPath; @@ -65,6 +69,7 @@ namespace kiwix { kiwix::Library library; bool readBookFromPath(const string path, Book &book); + bool parseXmlDom(const pugi::xml_document &doc, const bool readOnly); }; }