From 6cfc716c848e5399190b8cd89681582a6a73fa3d Mon Sep 17 00:00:00 2001 From: kelson42 Date: Wed, 4 May 2011 15:27:08 +0000 Subject: [PATCH] + new dev in content manager core --- src/common/kiwix/library.cpp | 4 +- src/common/kiwix/library.h | 3 ++ src/common/kiwix/manager.cpp | 74 +++++++++++++++++++++++++++--------- src/common/kiwix/manager.h | 5 ++- 4 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/common/kiwix/library.cpp b/src/common/kiwix/library.cpp index d9805f84c..f1acc49d8 100644 --- a/src/common/kiwix/library.cpp +++ b/src/common/kiwix/library.cpp @@ -43,7 +43,9 @@ namespace kiwix { } /* Constructor */ - Library::Library() { + Library::Library(): + current(""), + version(KIWIX_LIBRARY_VERSION) { } /* Destructor */ diff --git a/src/common/kiwix/library.h b/src/common/kiwix/library.h index f655c1ac9..f8ad18086 100644 --- a/src/common/kiwix/library.h +++ b/src/common/kiwix/library.h @@ -20,6 +20,8 @@ #ifndef KIWIX_LIBRARY_H #define KIWIX_LIBRARY_H +#define KIWIX_LIBRARY_VERSION "20110503" + #include #include @@ -57,6 +59,7 @@ namespace kiwix { ~Library(); string current; + string version; bool addBook(const Book &book); bool removeBookByIndex(const unsigned int bookIndex); vector books; diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index 61cf89666..3f6e6048c 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -37,6 +37,8 @@ namespace kiwix { 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; @@ -53,7 +55,16 @@ namespace kiwix { book.url = bookNode.attribute("url").value(); book.articleCount = bookNode.attribute("articleCount").value(); book.mediaCount = bookNode.attribute("mediaCount").value(); - library.addBook(book); + + /* 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); + } } } @@ -69,6 +80,9 @@ namespace kiwix { if (library.current != "") libraryNode.append_attribute("current") = library.current.c_str(); + + if (library.version != "") + libraryNode.append_attribute("version") = library.version.c_str(); /* Add each book */ std::vector::iterator itr; @@ -123,26 +137,37 @@ namespace kiwix { bool Manager::addBookFromPath(const string path, const string url) { kiwix::Book book; - /* Open the ZIM file */ - kiwix::Reader reader = kiwix::Reader(path); - book.path = path; - book.id = reader.getId(); - book.title = reader.getTitle(); - book.description = reader.getDescription(); - book.language = reader.getLanguage(); - book.date = reader.getDate(); - book.creator = reader.getCreator(); - book.url = url; - - std::ostringstream articleCountStream; - articleCountStream << reader.getArticleCount(); - book.articleCount = articleCountStream.str(); + if (this->readBookFromPath(path, book)) { + book.url = url; + library.addBook(book); + return true; + } - std::ostringstream mediaCountStream; - mediaCountStream << reader.getMediaCount(); - book.mediaCount = mediaCountStream.str(); + return false; + } + + bool Manager::readBookFromPath(const string path, kiwix::Book &book) { - library.addBook(book); + try { + kiwix::Reader reader = kiwix::Reader(path); + book.path = path; + book.id = reader.getId(); + book.title = reader.getTitle(); + book.description = reader.getDescription(); + book.language = reader.getLanguage(); + book.date = reader.getDate(); + book.creator = reader.getCreator(); + + std::ostringstream articleCountStream; + articleCountStream << reader.getArticleCount(); + book.articleCount = articleCountStream.str(); + + std::ostringstream mediaCountStream; + mediaCountStream << reader.getMediaCount(); + book.mediaCount = mediaCountStream.str(); + } catch (...) { + return false; + } return true; } @@ -151,6 +176,17 @@ namespace kiwix { return this->library.removeBookByIndex(bookIndex); } + bool Manager::removeBookById(const string id) { + unsigned int bookIndex = 0; + std::vector::iterator itr; + for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) { + if ( itr->id == id) + return this->library.removeBookByIndex(bookIndex); + bookIndex++; + } + return false; + } + kiwix::Library Manager::cloneLibrary() { return this->library; } diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index 4b887dda0..da9e879ae 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -39,11 +39,14 @@ namespace kiwix { bool readFile(const string path); bool writeFile(const string path); bool removeBookByIndex(const unsigned int bookIndex); + bool removeBookById(const string id); bool addBookFromPath(const string path, const string url = ""); - kiwix::Library cloneLibrary(); + Library cloneLibrary(); protected: kiwix::Library library; + + bool readBookFromPath(const string path, kiwix::Book &book); }; }