+ last imp. of contentManager

This commit is contained in:
kelson42 2011-05-06 19:45:25 +00:00
parent c7fbc52e7a
commit ca1713d609
4 changed files with 67 additions and 43 deletions

View File

@ -35,7 +35,8 @@ namespace kiwix {
creator(""),
url(""),
articleCount(""),
mediaCount("") {
mediaCount(""),
readOnly(false) {
}
/* Destructor */

View File

@ -50,6 +50,7 @@ namespace kiwix {
string url;
string articleCount;
string mediaCount;
bool readOnly;
};
class Library {

View File

@ -23,14 +23,15 @@
namespace kiwix {
/* Constructor */
Manager::Manager() {
Manager::Manager() :
writableLibraryPath("") {
}
/* Destructor */
Manager::~Manager() {
}
bool Manager::readFile(const string path) {
bool Manager::readFile(const string path, const bool readOnly) {
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(path.c_str());
@ -42,6 +43,7 @@ namespace kiwix {
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 = bookNode.attribute("last").value();
@ -67,6 +69,9 @@ namespace kiwix {
}
}
if (!readOnly)
this->writableLibraryPath = path;
}
return result;
@ -78,8 +83,9 @@ namespace kiwix {
/* Add the library node */
pugi::xml_node libraryNode = doc.append_child("library");
if (library.current != "")
if (library.current != "") {
libraryNode.append_attribute("current") = library.current.c_str();
}
if (library.version != "")
libraryNode.append_attribute("version") = library.version.c_str();
@ -87,6 +93,8 @@ namespace kiwix {
/* Add each book */
std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if (!itr->readOnly) {
pugi::xml_node bookNode = libraryNode.append_child("book");
bookNode.append_attribute("id") = itr->id.c_str();
bookNode.append_attribute("path") = itr->path.c_str();
@ -127,6 +135,7 @@ namespace kiwix {
bookNode.append_attribute("mediaCount") = itr->mediaCount.c_str();
}
}
/* saving file */
doc.save_file(path.c_str());
@ -200,4 +209,14 @@ namespace kiwix {
return this->library;
}
bool Manager::getBookById(const string id, Book &book) {
std::vector<kiwix::Book>::iterator itr;
for ( itr = library.books.begin(); itr != library.books.end(); ++itr ) {
if ( itr->id == id) {
book = *itr;
return true;
}
}
return false;
}
}

View File

@ -36,7 +36,7 @@ namespace kiwix {
Manager();
~Manager();
bool readFile(const string path);
bool readFile(const string path, const bool readOnly = true);
bool writeFile(const string path);
bool removeBookByIndex(const unsigned int bookIndex);
bool removeBookById(const string id);
@ -44,6 +44,9 @@ namespace kiwix {
string getCurrentBookId();
bool addBookFromPath(const string path, const string url = "");
Library cloneLibrary();
bool getBookById(const string id, Book &book);
string writableLibraryPath;
protected:
kiwix::Library library;